"value" property in Field is set to an state variable but it doesn't work

1 Answer 881 Views
Form Input
Mohammad
Top achievements
Rank 1
Mohammad asked on 27 Jun 2021, 10:47 AM

Hi, I'm building an update form and the goal is to load the current form data which we get from server in controls.

so I took a piece of code from the samples (here),

and I changed it in order to bind it to a property in state as below:

<Field
    name={"firstName"}
    component={FormInput}
    label={"someLabel"}
    value={this.state.firstName}
    onChange={(e) => this.setState({ firstName: e.target.value })}
/>

and my state is:

state = {
firstName: null
}

and I get some data from server and assign it to the state in componentDidMount method as below:

componentDidMount() {
        axios.get("someURL").then(response => {
            this.setState({ firstName: response.data })
        })
    }

the goal is to get the current value from the server and set it to the control

the problem is i don't see the values of the form when it's rendered.

Thanks

and i also attach the jsx file just in case

1 Answer, 1 is accepted

Sort by
1
Stefan
Telerik team
answered on 28 Jun 2021, 06:24 AM

Hello, Mohammad,

Thank you for the details.

The Form component is internally controlling the value of all Field components. This is why setting value and onChange outside of the Form is not recommended. The Form will do it automatically.

If the Form has to be populated after a request on a specific event, I can suggest checking the following example:

https://www.telerik.com/kendo-react-ui/components/form/advanced-scenarios/#toc-resetting-the-form-initial-state

I hope this is helpful.

Regards,
Stefan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Form Input
Asked by
Mohammad
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or