dillon1000/react
Commit
Browse files [Flight] Implement useId hook (#24172)
* Implements useId hook for Flight server. The approach for ids for Flight is different from Fizz/Client where there is a need for determinancy. Flight rendered elements will not be rendered on the client and as such the ids generated in a request only need to be unique. However since FLight does support refetching subtrees it is possible a client will need to patch up a part of the tree rather than replacing the entire thing so it is not safe to use a simple incrementing counter. To solve for this we allow the caller to specify a prefix. On an initial fetch it is likely this will be empty but on refetches or subtrees we expect to have a client `useId` provide the prefix since it will guaranteed be unique for that subtree and thus for the entire tree. It is also possible that we will automatically provide prefixes based on a client/Fizz useId on refetches in addition to the core change I also modified the structure of options for renderToReadableStream where `onError`, `context`, and the new `identifierPrefix` are properties of an Options object argument to avoid the clumsiness of a growing list of optional function arguments. * defend against useId call outside of rendering * switch to S from F for Server Component ids * default to empty string identifier prefix * Add a test demonstrating that there is no warning when double rendering on the client a server component that used useId * lints and gates
Changed paths8 files
First-parent comparisonpackages/react-client/src/__tests__/ReactFlight-test.js ModifiedM packages/react-noop-renderer/src/ReactNoopFlightServer.js ModifiedM packages/react-server-dom-relay/src/ReactFlightDOMRelayServer.js ModifiedM packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js ModifiedM packages/react-server-dom-webpack/src/ReactFlightDOMServerNode.js ModifiedM packages/react-server/src/ReactFlightHooks.js ModifiedM packages/react-server/src/ReactFlightServer.js ModifiedM scripts/error-codes/codes.json ModifiedPatch
Files changed
Rendering syntax-highlighted changes…