1

I have an array which is being used by another file to map out the contents. I originally hard coded values into this array, but now I'd like to integrate an axios get call to retrieve data, and then store the info from the response into the array. I can successfully get the JSON response with the correct data, but I am stuck on getting the JSON response data into the array. Any help would be greatly appreciated

let theArray = [
{
id: '',
name: '',
},]

useEffect(() => {
        axios
            .get(`/api/data`)
            .then(res => {
                //? need to store res.data.id to theArray.id, and res.data.name to theArray.name
            })
    }, [])
1
  • I think this Sylens proposed a good solution, I just posted a new structure below. but is just a matter of different structure @reef Commented Aug 14, 2019 at 19:34

4 Answers 4

4

You can simply push the response to the array, but you'd have to begin with an empty array, otherwise, the first element you have hardcoded will not have any data.

let theArray = []

useEffect(() => {
        axios
            .get(`/api/data`)
            .then(res => {
                const newItem = {
                  id: res.data.id,
                  name: res.data.name,
                };
                theArray.push(newItem);
             })
    }, [])
Sign up to request clarification or add additional context in comments.

3 Comments

I think this is a good solution, I just posted a new structure below. but is just a matter of different structure
Hello, I am quite new to React and was wondering, if you wanted to print this data under render(), could you do "theArray[0].id" ? Also, is useEffect simply just an additional function?
Is it possible I use this method in app.get(....) in expressjs also? is there any conflict with res.render or not?
3

Here another solution, I think @Sylens solution is a good one, this is just a matter of structuring your code as you want

let theArray = []

useEffect(() => {
        axios
            .get(`/api/data`)
            .then(res => {
                // object destructuring
                const { id, name } = res.data;
                theArray.push({ id, name })
            })
    }, [])

1 Comment

Hello, I am quite new to React and was wondering, if you wanted to print this data under render(), could you do "theArray[0].id" ? If not, how would I access this information?
1

If your data is in json format you should wait for the info to get parsed, something like this:

let theArray = [
    {
        id: '',
        name: '',
    },
];

useEffect(() => {
    axios
        .get(`/api/data`)
        .then(res => res.json())
        .then((result) => {
            theArray[0].id = result.id;
            theArray[0].name = result.name;
        })
}, []);

Edit: if you want to add the new data just push it to the array

theArray.push({
    id: result.id,
    name: result.name,
})

1 Comment

Hello, I am quite new to React and was wondering, if you wanted to print this data under render(), could you do "theArray[0].id" ? If not, how would I access this information?
0

If it's only modifying first element:

let theArray = [{ id: '',  name: '',},]

useEffect(() => {
        axios
            .get(`/api/data`)
            .then(res => {
                theArray[0].id = res.data.id
                theArray[0].name = res.data.name
            })
    }, [])

2 Comments

Hello, I am quite new to React and was wondering, if you wanted to print this data under render(), could you do "theArray[0].id" ? If not, how would I access this information?
You can do render () { return <div>{theArray[0].name}</div> } or render () { return theArray[0].map(d => <div key={d.id}>{d.name}</div>) }

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.