Reputation: 995
I'm using React Admin userRedirect hook and I'm redirection to a "page" that is a components. Is there a way to pass data to the page/component that I redirect to? I tried this and observe the following.
The page I'm redirecting to:
const PublicPage = (props) => {
console.log('data: ' + props.data);
return(
<Card>
<Title title="Public Page" />
<CardContent>
This is a public page. + {props.data}
</CardContent>
</Card>
)
}
If I do: redirect('/public', {data:'this is some data'}), the console log displays 'data: undefined' and no data is displayed.
Is this something doable with RA? Thanks for any suggestion you may have.
Christian
Upvotes: 1
Views: 1572
Reputation: 7335
You could redirect to a location with a query string (e.g. '/public?foo=bar') and read that query string from your PublicPage component.
Alternately, you can use the location state - some sort of data container provided by react-router. This isn't supported by useRedirect
, but it's not hard to do by hand:
import { useHistory } from 'react-router-dom';
const useRedirectToPageWithState = () => {
const history = useHistory();
return useCallback((pathname, state) => {
history.push({
pathname,
state
})
}, [history]);
}
Then in your PublicPage component, you can read the location state using useLocation:
const PublicPage = () => {
const { state: data } = useLocation();
// now data contains what you passed to the page
Upvotes: 3