1

I am trying to fetch an api with react hooks. I am able to get the data from the api and declared some attributes with useState. Now I am trying to set the attributes from the api to the attributes in useState, the same way you would do it with setState in class components, but I am not sure how to go about it.

enter image description here

enter image description here

Above is the userData that I did a console.log for in the promise.

1
  • useState returns 2 things, the current state and a function that you can use to update the state. Change const initialState = to const [state, setState] = and then call setState(userData) inside your then(userData => { function Commented Apr 1, 2020 at 21:35

2 Answers 2

1

useState returns an array where the first element is the data and the second is a function which you call anywhere to update the state later.

From your example:

    // Receive the array from useState and use the "array destructuring"
    const [myState, setMyState] = useState({/*... you data */});

    // ...
    useEffect(() => {
        // fetch...
        // .then...
        .then(userData => {
            console.log(userData);
            setMyState(userData); // call the function here
        });
    // ...

This should be enough to have your state object updated!

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

Comments

0

You need to make a few changes, first is that the initial state should be an array

const [initialState, setInitialState] = useState([])

I would use an array since the response is giving you an array of users, in your then statement where you have the console.log you can do

setInitialState(userData)

That will update the initialState value with the array of items, if you actually plan to use just one user just pick one from the userData array. The setInitialState will be the function you need to use when you plan to use the initialState value.

7 Comments

will this work if the data I have consists of multiple attributes? what I want to do is create an initial state for them and then set the state with the data from userData... the name email id etc...
Based on the second picture you receive an array of user items, so you should store it like an array, unless you want to grab one of the users that are in that array, if that's the case you should do something like setInitialState(userData[0]) and that will use the user that is at first on your array for example.
So if you want an specific user from that array, you need to use the find method, for example setInitialState(userData.find(user => user.name === "John") || {}), that should give you the user whose name is John, and so on.
Yes that’s correct, you can do the same for other properties as well or if not you can pass just one user prop, it just depends how you want to handle that.
For example assuming the user in already in the initialState variable you can pass it to a child like you suggested something like <Child name={initialState.name} /> and that will do
|

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.