2

Let's suppose an API call is made at the backend, a value is being fetched from the API, and this value needs to be shown inside the input tag (jsx). How to show that value?

1
  • So you make the call and set a property/state.... Commented Nov 20, 2019 at 20:31

2 Answers 2

2

You just need to set it your component's state using the setState function. A possible scenario might be:

componentDidMount() {
  fetchSomeData().then((data) => {
    this.setState({value: data})
  });
}
/* ... later, in your render function */
<input value={this.state.value} />

For more information on how setState works (and to learn more about the entire React lifecycle), see https://reactjs.org/docs/react-component.html#setstate

Sign up to request clarification or add additional context in comments.

Comments

0

All you need is to initialize a state. After fetching your api call, update the state and you can use that state in input value:

<input value={this.state.value} />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.