简体   繁体   English

如果在反应中添加子组件,useEffect 在父组件中不起作用

[英]useEffect is not working in parent Component if child component is added in react

I want to pass data to child component after getting data from API, if I remove the child component its working我想在从 API 获取数据后将数据传递给子组件,如果我删除子组件它的工作

const parentComponent= (props) => {
  const [loans, setLoans] = useState({});

  useEffect(() => {
    axios.get(`api`).then((result) =>{ 
      let { data } = result;
      console.log('result', result)      
      setLoan(data);  
    });
  }, []);

 return(
  <ChildComponent data={loans} />
)
}

You are not setting the to the state loans .您没有设置为 state loans You have to use setLoans instead of setLoan .您必须使用setLoans而不是setLoan Check this, it should work now.检查这个,它现在应该可以工作了。

const parentComponent= (props) => {
  const [loans, setLoans] = useState({});

  useEffect(() => {
    axios.get(`api`).then((result) =>{ 
      let { data } = result;
      console.log('result', result)      
      setLoans(data);  
    });
  }, []);

 return(
  <ChildComponent data={loans} />
)
}

const [loans, setLoans] = useState({}); use setLoan or changed name after console.log.在 console.log 之后使用 setLoan 或更改名称。 You have done setLoans which is not equal to setLoan您完成的 setLoans 不等于 setLoan

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

相关问题 React hooks 子组件 useEffect 在父组件之前先执行 - React hooks child component useEffect executes first, before parent component 在父组件中反应子组件 - React child Component in parent Component React Native:为什么子组件可以访问父组件中 useEffect 内的变量? - React Native:Why does the child component have access in a variable which is inside the useEffect in a parent component? 在React中将子组件的值传递给父组件 - Pass value of child component to parent component in React React父级组件道具未传递给子级组件 - React parent component props are not passing to child component React:子组件过滤父组件道具 - React: Child Component Filtering Parent Component Props (React)子组件在父组件重新渲染时暂停其 useEffect - (React)Child component pause its useEffect when the parent re-renders React router6:重定向到子路由也会执行父组件并且它是useEffect多次 - React router6: Redirecting to child route also executes the parent component and it's useEffect multiple times 使用回调和挂钩(useState,useEffect)从父级中的 onClick 事件更改 React 子组件的 state - Change React child component's state from an onClick event in parent, with callbacks and hooks (useState, useEffect) React Native - 大于父组件的子组件上的 onPress 无法在 Android 上运行 - React Native - onPress on child larger than parent component not working on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM