简体   繁体   English

React Hook useEffect 缺少依赖项:'getNewPins'

[英]React Hook useEffect has a missing dependency: 'getNewPins'

I have a small project, to fetch images from Unsplash, even ı check many times, I still have one problem with my code, I am running my code, but it always gives this error, but I don't get it.我有一个小项目,要从 Unsplash 获取图像,即使我检查了很多次,我的代码仍然有一个问题,我正在运行我的代码,但它总是给出这个错误,但我不明白。 Any idea will be appreciated.任何想法将不胜感激。

App.js:应用程序.js:

function App() {
    const [pins, setNewPins] = useState([])
    const getImages = (term) => {
        return unsplash.get ("https://api.unsplash.com/search" , {
            params: {
              query: term
            }
        });
    };
    const onSearchSubmit = (term) => {
        getImages(term).then((res) => {
            let results = res.data.results;
            let newPins = [
              ...results,
              ...pins
            ]
            newPins.sort(function(a,b) {
                return 0.5 - Math.random();
            });
            setNewPins(newPins);
        });
    };
    const getNewPins = () => {
        let promises = [];
        let pinData = [];
        let pins = ['Istanbul','cats','sky','lake','nature']
        pins.forEach((pinTerm) => {
            promises.push (
              getImages(pinTerm).then((res) => {
                  let results = res.data.results;
                  pinData = pinData.concat(results);

                  pinData.sort(function(a,b) {
                      return 0.5 - Math.random();
                  });
              });
            );
        });
        Promise.all(promises).then(()=> {
            setNewPins(pinData);
        });
};
useEffect(() => {
    getNewPins();
}, []);
return (
    <div className="app">
        <Header  onSubmit={onSearchSubmit}/>
            <Mainboard  pins={pins}/>
    </div>
)}
export default App;

The getNewPins method is re-created everytime in your app after each render and you're using it inside an useEffect which is saying to place it inside the [] array. getNewPins方法每次渲染后都会在您的应用程序中重新创建,并且您在useEffect中使用它,这表示将其放置在[]数组中。 If you do so, it will later say to wrap getNewPins inside useCallback to prevent re-renders.如果你这样做,它稍后会说将getNewPins包装在useCallback中以防止重新渲染。 Instead of that you can just place the whole function inside your useEffect like so (since it's a one time operation that's done initially):-取而代之的是,您可以像这样将整个 function 放在您的useEffect (因为它是最初完成的一次性操作):-

function App() {
const [pins, setNewPins] = useState([])
const getImages = (term) => {
return unsplash.get ("https://api.unsplash.com/search" , {
params: {
  query: term
}});
};
const onSearchSubmit = (term) => {
 getImages(term).then((res) => {
 let results = res.data.results;
 let newPins = [
   ...results,
   ...pins,
 ]
 newPins.sort(function(a,b){
  return 0.5 - Math.random();
 });
 setNewPins(newPins);
});
};

  useEffect(() => {
   const getNewPins = () => {
 let promises = [];
 let pinData = [];
  let pins = ['Istanbul','cats','sky','lake','nature']
  pins.forEach((pinTerm) => {
  promises.push (
  getImages(pinTerm).then((res) => {
  let results = res.data.results;
  pinData = pinData.concat(results);

  pinData.sort(function(a,b){
    return 0.5 - Math.random();
  });
 })
 );
});
Promise.all(promises).then(()=> {
 setNewPins(pinData);
 });
 };
  getNewPins();
 }, []);
return (
<div className="app">
  <Header  onSubmit={onSearchSubmit}/>
  <Mainboard  pins={pins}/>
</div>
);
}
export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM