|
|
|
|
|
by chrismorgan
1494 days ago
|
|
In general I would agree, but as soon as you’re using popstate, you’ve broken that completely: it’s inherently global and will conflict with any other compositions. Because of this, the use of a global click handler will make the application more consistent: either all forms of navigation work, or none; whereas if you use <Link> it gives the impression that you can compose, but the back and forward buttons may be broken. The History API, as it stands, is global state. That makes it one of the very few places where I say it is better to use a global solution, rather than a precise one. |
|
It can be problematic to access globals from functional programming contexts, but I don't really see how History would cause conflicts here. Do you have any examples?
If you are really worried about this then the proper way to solve it imo is to pass `history` in as a prop or Context instead of calling it from the global `window` directly. This follows functional paradigms and is more in line with React principles. This has some other advantages as well. For example, if you are using test frameworks like Jest, you can test the link component by mocking the History object being passed in.