[英]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.