-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Closed
Description
Ref #6362 (comment)
Since "always render" behaviour will be removed from <Route children />
it would be good to provide a way to access context values from any part of app.
Here's a few ways and all requires to provide RouterContext
.
RouterContext.Consumer
const Component = () => (
<RouterContext.Consumer>
{context => (
<button onClick={() => {
if (context) {
context.history.push('/users')
}
}}>Open users</button>
)}
</RouterContext.Consumer>
)
Class component contextType
const Component = class extends React.Component {
contextType = RouterContext;
handler = () => {
this.context.history.push('/users')
}
}
context.unstable_read api
const Component = () => {
const context = RouterContext.unstable_read();
return (
<button onClick={() => {
if (context) {
context.history.push('/users')
}
}}>Open users</button>
)
}
fi3ework
Metadata
Metadata
Assignees
Labels
No labels