Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The library works like this: there is an outer context (issued via the
FootnotesProvider
component). Then every footnote reference being rendered within that context (with theFootnoteRef
component) is tracked, in the correct order. From this collection of references can be automatically generated the actual footnotes (theFootnotes
component), at the bottom of the page. See the example for more details.Current problem
In the current version, the footnote references register themselves within the context when mounting:
react-a11y-footnotes/src/index.js
Line 29 in 5ddb1aa
The problem is that it of course doesn’t work with server-side rendering because
React.useEffect()
does not fire on the server. As a result, the footnote references are being rendered correctly, but they don’t link to anything because no footnotes are effectively recorded in the context.Suggested solution
This pull-request replaces the state provided through the context with a React ref so it works on the server. So instead of having a
footnotes
array in the local state and passing it down (and its setter) to theFootnotesContext
context, theFootnotesProvider
initialises an empty map in a React ref, and passes that ref down.🤔 Now what I’m not sure about is whether having this ref being mutated in the render method of every footnote reference is a good idea. Basically if they don’t find themselves already in the array, they push themselves in it. It feels a little shady having to do that in the render method.
I’m open to suggestions. :)
The current (state) implementation can be played with here: https://codesandbox.io/s/footnotes-v34hm
The new (ref) implementation can be played with here: https://codesandbox.io/s/footnotes-forked-e3t4f