Javascript 使用位置不';我无法识别我发送的状态

Javascript 使用位置不';我无法识别我发送的状态,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我开始使用react路由器,我发现我可以在链接组件中传递“道具”,这样一些值就可以传递到另一个组件。我正在使用的按钮中发送一个名为“value”的组件,但是在接收该参数的组件中,会显示一条错误消息,消息为“Object可能为null或未定义” 这是我的密码: 我将数据发送到的位置: <Container placeholder> <Segment> <Form> <Form.

我开始使用react路由器,我发现我可以在链接组件中传递“道具”,这样一些值就可以传递到另一个组件。我正在使用的按钮中发送一个名为“value”的组件,但是在接收该参数的组件中,会显示一条错误消息,消息为“Object可能为null或未定义”

这是我的密码:

我将数据发送到的位置:

<Container placeholder>
            <Segment>
            <Form>
                <Form.Field>
                    <label>Correo</label>
                    <input placeholder='Ingresa tu correo' name='nombre' onChange={actualizarUser}/>
                </Form.Field>
                <Form.Field>
                    <label>Contraseña</label>
                    <input placeholder='Ingresa tu contraseña' name='password' onChange={actualizarUser}/>
                </Form.Field>

                <Link to={{ pathname:'/init/home', state:{ value: token } }}> // Here I'm sending the props to the next component
                    <Button type='submit'>SubmitNuevo</Button>
                </Link>
                <Button type='submit' onClick={sendInfo}>Prueba</Button>
            </Form>
            </Segment>
        </Container>

科雷奥
康瑟斯尼娜
//这里我将道具发送到下一个组件
SubmitNuevo
普鲁巴
以及接收location.state的组件

const Logged: React.FC<{}> = () => {

const [open, setOpen] = useState(false);  
const location = useLocation(); // Here I'm using useLocation to capture the props I sent
const [token, setToken] = useState('');

useEffect(() => {
        console.log(location.state);
        setToken(location.state.value); // Here is were I'm getting the error message
        console.log(token);
});

const handleOpen = () => {
    setOpen(true);
}

const handleClose = () => {
    setOpen(false);
}

return(<div>

    </div>
);
const-Logged:React.FC=()=>{
const[open,setOpen]=useState(false);
const location=useLocation();//这里我使用useLocation来捕获我发送的道具
const[token,setToken]=useState(“”);
useffect(()=>{
console.log(location.state);
setToken(location.state.value);//下面是我收到的错误消息
console.log(令牌);
});
const handleOpen=()=>{
setOpen(真);
}
常量handleClose=()=>{
setOpen(假);
}
返回(
);

感谢您的帮助

您需要使用
props.location
而不是
useLocation();
。您能试试这个吗

const-Logged:React.FC=(道具)=>{
const[open,setOpen]=useState(false);
const[token,setToken]=useState(“”);
useffect(()=>{
日志(props.location.state);
setToken(props.location.state.value);//下面是我收到的错误消息
console.log(令牌);
});
const handleOpen=()=>{
setOpen(真);
}
常量handleClose=()=>{
setOpen(假);
}
返回(

)
您是否使用
SubmitNuevo
按钮来导航
记录的
路线中呈现的
以获得路线道具?