javascript - Unable to copy array using setstate hook - Stack Overflow

I am fetching data from backend using axios whenever I am trying to update hooks it is not updating.Th

I am fetching data from backend using axios whenever I am trying to update hooks it is not updating. The data is JSON from where I am extracting data and trying to set element. It might sound silly but can somebody tell me what is dependent array?

I keep getting this
Line 18: React Hook useEffect has a missing dependency: 'elements'. Either include it or remove the dependency array react-hooks/exhaustive-deps

Here is code

import React, { useEffect, useState } from 'react';
import './App.css';
import axios from 'axios';

function App() {
  const [elements, setElements] = useState([]);

  useEffect(() => {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      console.log(data);
      setElements(elements => [...elements, data]);
      console.log(elements);
    };
    res();
  }, []);
 console.log(elements.map(element => console.log(element)));
  return <div className='App'>Hello</div>;
}

export default App;

I am fetching data from backend using axios whenever I am trying to update hooks it is not updating. The data is JSON from where I am extracting data and trying to set element. It might sound silly but can somebody tell me what is dependent array?

I keep getting this
Line 18: React Hook useEffect has a missing dependency: 'elements'. Either include it or remove the dependency array react-hooks/exhaustive-deps

Here is code

import React, { useEffect, useState } from 'react';
import './App.css';
import axios from 'axios';

function App() {
  const [elements, setElements] = useState([]);

  useEffect(() => {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      console.log(data);
      setElements(elements => [...elements, data]);
      console.log(elements);
    };
    res();
  }, []);
 console.log(elements.map(element => console.log(element)));
  return <div className='App'>Hello</div>;
}

export default App;
Share Improve this question edited Oct 1, 2019 at 19:14 vinayak shahdeo asked Oct 1, 2019 at 18:32 vinayak shahdeovinayak shahdeo 1,4882 gold badges13 silver badges23 bronze badges 2
  • Normally when you set state after async function finishes you have to check if the ponent is mounted before you set the state – HMR Commented Oct 1, 2019 at 18:49
  • thanks for the tip – vinayak shahdeo Commented Oct 1, 2019 at 19:15
Add a ment  | 

3 Answers 3

Reset to default 5

Just console.log outside your effect. You're already using the updater version of useState

 setElements(elements => [...elements, data])

The missing dependecy warning is ing from console.log(elements)

import React, { useEffect, useState } from 'react';
import './App.css';
import axios from 'axios';

function App() {
  const [elements, setElements] = useState([]);

  useEffect(() => {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      console.log(data);
      setElements(elements => [...elements, data]);
    };
    res();
  }, []);

  console.log(elements);

  return <div className='App'>Hello</div>;
}

export default App;

Missing dependency warning is because you use console.log(elements) inside the useEffect. And your elements log is not showing latest result because state is not changed (yet)

Just add a useEffect to keep track of elements changes like below.

 function App() {
  const [elements, setElements] = useState([]);

  useEffect(() => {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      console.log(data);
      setElements(elements => [...elements, data]);
    };
    res();
  }, []);

  useEffect(() => console.log(elements), [elements])

  return <div className='App'>Hello</div>;
}

export default App;

To answer your question;

The dependency array is their to let React know when the useEffect in this case should be triggered. So the useEffect i added, only triggers when its dependency elements is changed.

In your case you are puting the array data inside elements, setElements(elements => [...elements, data]); so it will be array inside array. Try the below :

function App() {
  const [elements, setElements] = useState([]);
  useEffect(() => {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      console.log(data);
      setElements([...elements, data]);
    };
    res();
  }, []);

  useEffect(() => console.log(elements), [elements])

  return <div className='App'>Hello</div>;
}

export default App;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742351878a4427682.html

相关推荐

  • javascript - Unable to copy array using setstate hook - Stack Overflow

    I am fetching data from backend using axios whenever I am trying to update hooks it is not updating.Th

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信