React Form - Update Form Data Using the API
If you need to update form data at runtime, bind the formData property to a state property. Swapping the whole formData object rerenders the UI component from scratch; updating specific formData fields rerenders only the corresponding editors.
App.js
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import { Form } from 'devextreme-react/form';
- import { Button } from 'devextreme-react/button';
- const employee = {
- firstName: 'John',
- lastName: 'Heart',
- phone: '+1(213) 555-9392'
- };
- class App extends React.Component {
- constructor() {
- super();
- this.state = {
- employee
- }
- this.updatePhone = this.updatePhone.bind(this);
- }
- render() {
- return (
- <div>
- <Form formData={this.state.employee} />
- <Button
- text="Update the Phone Number"
- onClick={this.updatePhone} />
- </div>
- );
- }
- updatePhone(e) {
- this.setState(() => {
- return { employee: {...employee, phone: '+1(333) 888-7698' } };
- });
- }
- }
- export default App;