Home > database >  How to change a snippets of code from Class Component to Functional Component
How to change a snippets of code from Class Component to Functional Component

Time:11-14

Here is the snippets code I want to change it to Functional component,

    this.setState({ data, query: text },
 () => this.makeRemoteRequest());

I changed it but not working, please ask me a question,

This way it's not working.

setQuery(data, text, () => makeRemoteRequest());

CodePudding user response:

You can have something like the following.

const [query, setQuery] = useState();
const [data, setData] = useState();

useEffect(() => {
  makeRemoteRequest();
}, [query])

Read more about useEffect here

CodePudding user response:

You're trying to make a set of data and text, then call a callback after the set.

There are several ways to obtain this behaviour.

What I would suggest you is to have a state (useState) which include data and text and then listen for the changes of this stage through a useEffect.

  • Related