当我单击其中一条路线 ( ) 的链接时,我想在两条路线之间共享状态NewUser。我想要共享的状态和修改它的逻辑都保存在路由中Users。我想将改变状态的逻辑传递给路由NewUsers。
当我将字符串传递给 router 中的状态对象时Link,我可以在NewUsers组件中访问它。但是,null当我传递一个函数时,我得到了。
我知道我可以使用 context/redux,但我更愿意这样做。
Users路线:
function Users() {
const [users, setUsers] = useState([]);
return (
<Card sx={{ padding: "2rem", mt: "2rem" }}>
<MDBox
display="flex"
flexDirection="row"
justifyContent="space-between"
>
<MDTypography variant="body2">{`You currently have ${users.length} users`}</MDTypography>
<MDButton variant="gradient" color="info" size="small">
<Link to="/settings/users/new-user" state={setUsers: setUsers}> //this is how I want to pass the state
<MDBox
display="flex"
alignItems="center"
color="white"
fontWeight="normal"
>
<Icon>add</Icon> Add New User
</MDBox>
</Link> …Run Code Online (Sandbox Code Playgroud)