I started to use react-router and I find out that I can pass 'props' in Link component so some values can pass to another component. I'm sending a component called 'value' inside the button I'm using, however in the component that receive that parameter show an error message with the message 'Object is possibly null or undefined'.
Here is my code:
Where I'm sending the data:
<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>
And the component where I receive 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>
);
I also tried to manage that as props, however there's another error that show it as if location wasn't recognized:
Here is the information of the second option about where I'm using Route and props to pass the information
function App() {
return (
<div className="App">
<Navbar bg="dark" variant="dark">
<Navbar.Brand href="#home">
Micrin
</Navbar.Brand>
</Navbar>
<Router>
<Switch>
<Route path='/login' component={InicioSesion} />
<Route path='/register' component={Registro} />
<Route path='/recover' component={RecuperarCuenta}/>
<Route path='/init/home' exact component={Logged} />
<Route path='/init/stock' exact component={Logged} />
<Route path='/init/menu' exact component={Logged} />
<Route path='/init/sales' exact component={Logged} />
<Route path='/init/market' exact component={Logged} />
<Route path='/' component={MainPage} />
</Switch>
</Router>
</div>
);
}
And the component rendering with props I sent
const Logged: React.FC<{}> = (props) => {
const [open, setOpen] = useState(false);
const [token, setToken] = useState('');
useEffect(() => {
console.log(props.location.state);
setToken(props.location.state.value); // Shows error message 'Property location does not exist on type {children?: ReactNode}'
console.log(token);
});
const handleOpen = () => {
setOpen(true);
}
const handleClose = () => {
setOpen(false);
}
return(
<div></div>
);
Thank you for your help


