简体   繁体   English

如何在 useState 挂钩中将道具传递给初始 state?

[英]how to pass props to initial state in useState hook?

I am having an issue with initialising a useState hook with props that is passed to the current component from another.我在使用从另一个传递给当前组件的道具初始化 useState 挂钩时遇到问题。

the areas where I am having the issue is below (Keep in mind that I can access the prop if i just console.log it, so it not a matter of a failure of the prop passing data down)我遇到问题的区域如下(请记住,如果我只是 console.log 就可以访问道具,所以这不是道具传递数据失败的问题)

const [ Uid, setUid ] = useState('');

It is important for me to pass the props like below as my data fetching on this component depends on Uid.传递如下属性对我来说很重要,因为我在此组件上获取的数据取决于 Uid。 So far I have tried到目前为止我已经尝试过

const [ Uid, setUid ] = useState(props.uid);

this did not work as it told me- first argument to be of type non-empty string, but it was: undefined这不起作用,因为它告诉我 - 第一个参数是非空字符串类型,但它是:未定义

const [ Uid, setUid ] = useState(props);

        useEffect(() => {
            setUid(props.uid);
        }, []);

Can anyone see where i might be going wrong?谁能看到我可能出错的地方?

the full code is here- https://www.codepile.net/pile/m3zmwnK1完整代码在这里-https://www.codepile.net/pile/m3zmwnK1

remember, the most important thing for me is to have the Uid contain the value from the props.uid as otherwise react gives me a "first argument to be of type non-empty string, but it was: undefined" issue请记住,对我来说最重要的是让 Uid 包含来自 props.uid 的值,否则反应会给我一个“第一个参数类型为非空字符串,但它是:未定义”问题

You are ignoring props.uid in subsequent effects.您在后续效果中忽略了 props.uid。 Try this尝试这个

const Component({uid}){
 const [stateUid, setUid] = useState(uid);

useEffect(()=>{
 setUid(uid);
},[uid])

}

You might want to add props to your useEffect dependency.您可能希望将道具添加到您的useEffect依赖项。 If it changes too often, u might drop into infinite loop如果它变化太频繁,你可能会陷入无限循环

useEffect(() => {
    setUid(props.uid);
}, [props.uid]);

In you App.js在你 App.js

function App() {
const [ user, setuser ] = useState(false);
const [ uid, setUid ] = useState(); //initial is empty

useEffect(() => {
    const unsubscribe = onAuthStateChange();
    return () => {
        unsubscribe();
    };
}, []); // it only run once so it might not set the state of uid

return <div className="App">{user ? <Home uid={uid} /> : <Form />}</div>; 
// uid passing to Home component might be empty

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

相关问题 如何使用 useState 钩子设置初始状态 - How to set an initial state using the useState hook 如何在笑话和酶中为 useState Hook 设置初始状态值? - How to set initial state value for useState Hook in jest and enzyme? 如何在 jest 和 enzyme 中为 useState Hook 设置初始 state? - How to set initial state for useState Hook in jest and enzyme? 如何将 useState 钩子传递给功能组件中的子道具 - How to pass useState hook to child props in functional components 如何在 React useState Initial State 中传递 object 的副本 - How to pass copy of object in React useState Initial State 如何使用 useState 钩子更新状态 - How to update state with the useState hook 如何将初始状态作为我的高阶组件的道具传递? - How to pass the initial state as props for my Higher Order Component? Redux:如何将父项道具传递到减速器的初始状态? - Redux: How to pass parent props into the initial state of a reducer? 如何使用 useEffect 挂钩中设置的另一个 state 的值设置 useState 的初始值? - How to set the initial value of useState with the value of another state set in a useEffect hook? 当一个初始状态用于通过 useState 钩子一次启动多个状态时,如何单独设置状态? - How to set states separately when an initial state is used to initiate several states at once with useState hook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM