Reactjs 将函数传递给子reac
我是新来的。我尝试将信息从子组件传播到父组件: 家长:Reactjs 将函数传递给子reac,reactjs,Reactjs,我是新来的。我尝试将信息从子组件传播到父组件: 家长: export default function App() { const [isLoading, setisLoading] = useState(false); return ( <div className="App"> {isLoading ? <LoadingView/> : <MainView SetStateOfLoading={setisLoa
export default function App() {
const [isLoading, setisLoading] = useState(false);
return (
<div className="App">
{isLoading ? <LoadingView/> : <MainView SetStateOfLoading={setisLoading}/>}
</div>
);
}
为什么我会收到这样的警告:
Warning: Cannot update a component (`App`) while rendering a different component (`GetInformation`). To locate the bad setState() call inside `GetInformation`, follow the stack trace as described in https:
//fb.me /setstate-in-render
in GetInformation (at App.js:16)
in div (at App.js:15)
in App (at src/index.js:9)
in StrictMode (at src/index.js:8)
?
在我的第一个小程序中,我想有一个按钮,当sombody单击它时,我们调用了一些函数来进行计算。
我希望当我们进入函数时,屏幕将变为加载动画,当我离开函数时,我们将进入formar屏幕
在react中正确的做法是什么?您的孩子应该是这样的
export default function GetInformation(props) {
console.log(typeof props.SetStateOfLoading);
props.SetStateOfLoading();
console.log("SetStateOfLoading run");
return (
<h2>This supposed to be main view </h2>
);
}
导出默认函数GetInformation(props){
控制台日志(道具类型、装载状态);
props.setStateofLoad();
日志(“SetStateOfLoading run”);
返回(
这应该是主视图
);
}
你错了
export default function GetInformation(SetStateOfLoading) {
相反,你的意思可能是
export default function GetInformation({ SetStateOfLoading }) {
jsx标记通过包含属性的对象作为其第一个参数传递
请看您刚刚犯了一个新员工常犯的错误 React组件将道具作为
对象
export default function GetInformation({ SetStateOfLoading }) {} // Object de-structuring
或者说清楚一点
export default function GetInformation({ props}) {
const SetStateOfLoading = props.SetStateOfLoading;
}
什么意思?这是Paernt@development如果有帮助,请标记为答案和投票
export default function GetInformation({ SetStateOfLoading }) {} // Object de-structuring
export default function GetInformation({ props}) {
const SetStateOfLoading = props.SetStateOfLoading;
}