New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate history object to its own context #7103
Conversation
Besides useHistory, are there other places we can use the HistoryContext instead of RouterContext? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's so embarrassing I missed that.
Other components are still relying on other properties from the main context. I think I can separate other context properties as well, so there will be separate context for each properties: history, location, match, and staticContext. Should I do that now or wait until this branch get merged? Edit: Forgot to check react-router-dom, found some potential candidates. |
Hi, Sorry for not being much present in the process of my issue the last weeks. @illuminist about separating each props into their own contexts i'm not sure of the benefit it would give since the original issue was about As for the Since the original issue was just about the Great work :) |
When will this be merged? |
Also wondering when this will get merged. We're experiencing a lot of component re-renders due to |
@timdorr There isn't much need to change right now from last time I committed. Do you think is it good to merge? |
+1 for merge |
Yeah, let's do it. I'll see if I can get this out soon. |
What if I want to force component rerendering on history changes while maintaining useHistory? |
@Kutalia The Hence if you were relying on |
I need to track
But I am afraid forcing component rerendering in a such way is not a recommended pattern. |
@Kutalia Another workaround is just putting But the best solution should be creating a custom hook that provide the information about history object. const useHistoryState = () => {
const history = useHistory()
const [index, setIndex] = useState(history.index)
const [length, setLength] = useState(history.length)
useEffect(()=> {
return history.listen(() => {
setIndex(history.index)
setLength(history.length)
})
}, [history])
return { index, length }
} You can add more property from history object as you like. |
Thanks for the suggestion. As I actually use |
Reference #6999
As a suggestion from @MeiKatz . I added a new context for history object only to be used for useHistory hook and prevent the hooks from getting update triggerred by the original context.
The original context still has all the same properties without any change, the history object is also in the original context.
Tested with my project and it reduced rerendering greatly.