将数据从较低组件传递到较高组件



这是我的层次

- AddEmployee component
- UpdateComponent
- Home
* EmployeeItem component 

the employee item component is just a line in a list包含许多保存员工数据的列,在这些列中,我有一个更新列,它是重定向到更新组件的图标,我如何将员工的数据传递给UpdateEmployee component以显示并允许修改它们,我不能使用react router历史对象,因为它不是路由,所以在EmployeeItem组件中未定义,react路由器将忽略它。有没有一种方法可以在不使用readux或其他状态管理器的情况下做到这一点,我一直在考虑useContext,但我不清楚如何做到。

我正在使用react路由器来处理重定向

要将数据从父级传递给子级,只需在组件本身的呈现内部传递即可。这些被称为道具。

<EmployeeItem name = {Lyes} city = {Lisbon}/>

然后在组件内部

const EmployeeItem = props => {
return (
<h1>Employee {props.name} is from {props.city}</h1>
);
}

最新更新