Reactjs 使用React钩子将数据从子级传递给其他子级

Reactjs 使用React钩子将数据从子级传递给其他子级,reactjs,react-router,react-hooks,Reactjs,React Router,React Hooks,这就是将数据传递给其子函数的父函数: function Parent() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); return ( <div className="Parent"> <BrowserRouter>

这就是将数据传递给其子函数的父函数:

function Parent() {

  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");


  return (
    <div className="Parent">
      <BrowserRouter>

          <Route exact path="/child1" render={ () =>
             <Child1 setUsername={setUsername} setPassword={setPassword} password={password} username={username}/> 
             />

          <Route exact path="/child2" 
            render={() =>
             <Child2 username={username} password={password}/>
            }/>

        </BrowserRouter>
    </div>
  );
}

export default Parent;
Child2:

const Child2 = (username,password) => {


    useEffect(() => {
         console.log("Username: "+username);
         console.log("Password: "+password);
    }, [])

    return (
        <div >
           Child 2 Page
        </div>
    )
}

export default Child2

const Child2=(用户名、密码)=>{
useffect(()=>{
console.log(“用户名:”+Username);
console.log(“密码:”+密码);
}, [])
返回(
儿童2页
)
}
导出默认儿童2

你忘记了
{
}

解释

您将在中的
props
对象中从父对象获取道具

const Child2 = (props) => {...}
您可以使用
props.*
访问任何父级props.
,也可以使用destructuring方法

const Child2 = ({username,password}) => {...}

您忘记了
{
}

解释

您将在中的
props
对象中从父对象获取道具

const Child2 = (props) => {...}
您可以使用
props.*
访问任何父级props.
,也可以使用destructuring方法

const Child2 = ({username,password}) => {...}

这是因为您试图
控制台。将
对象
记录为
字符串
,当您使用
'a string'+某种类型的对象
时,
javascript
将尝试在
某种类型的对象
中查找
toString()
函数,如果找不到它,它将作为
[object object object object]输出

因此,您需要将代码更改为以下内容:

// use proper destructuring
const Child2 = ({username,password}) => {


    useEffect(() => {
         console.log("Username: ", username);
         console.log("Password: ", password);
        // remove useEffect dependency so you can see the data in every render - maybe the first time is just undefined
    })

    return (
        <div >
           Child 2 Page
        </div>
    )
}
//使用适当的解构
const Child2=({username,password})=>{
useffect(()=>{
日志(“用户名:”,用户名);
console.log(“密码:”,密码);
//删除useEffect依赖项,以便可以在每次渲染中看到数据-可能第一次只是未定义
})
返回(
儿童2页
)
}

这是因为您试图
控制台。将
对象
记录为
字符串
,当您使用
'a string'+某种对象
时,
javascript
将尝试在
某种对象
中查找
toString()
函数,如果找不到它,则只输出为
[对象对象]

因此,您需要将代码更改为以下内容:

// use proper destructuring
const Child2 = ({username,password}) => {


    useEffect(() => {
         console.log("Username: ", username);
         console.log("Password: ", password);
        // remove useEffect dependency so you can see the data in every render - maybe the first time is just undefined
    })

    return (
        <div >
           Child 2 Page
        </div>
    )
}
//使用适当的解构
const Child2=({username,password})=>{
useffect(()=>{
日志(“用户名:”,用户名);
console.log(“密码:”,密码);
//删除useEffect依赖项,以便可以在每次渲染中看到数据-可能第一次只是未定义
})
返回(
儿童2页
)
}

我不敢相信……事情这么简单,但我的眼睛没有注意到,我认为我做错了什么。再次感谢,也感谢你的解释!我马上就要接受答案:)我不敢相信……事情这么简单,但我的眼睛没有注意到,我认为我做错了什么。再次感谢谢谢你的解释!我马上就要接受答案了:)谢谢你的回答。沙阿先回答了,但谢谢你们两个。我不敢相信我没有看到。谢谢你的回答。沙阿先回答了,但谢谢你们两个。我不敢相信我没有看到。