-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Fix shallow renderer not allowing hooks in forwardRef render functions #15100
Fix shallow renderer not allowing hooks in forwardRef render functions #15100
Conversation
The function passed to |
I wasn't quite sure about this either since it's named I was somewhat happy about the reduced nesting in react-devtools compared to class components. Wouldn't like to see this pattern disappear since it's yet another intermediate component one would have to create just to get to the DOM node (or an imperative handle for that matter). |
Some additional context why I thought components would be fine: The docs mention that I should use a named function to improve devtools experience. However, there's no concrete example for this (only the generic It was very tempting to just React.forwardRef(function Button(props, ref) {
// some additional behavior
return <button ref={ref} {...props} />
}); and the devtools would show This is also a neat pattern if reactjs/rfcs#107 is accepted since I can just remove the -React.forwardRef(function Button(props, ref) {
+function Button(props) {
// some additional behavior
- return <button ref={ref} {...props} />
+ return <button {...props} />
-});
+} If this is actually undesired behavior that function components can be passed into
|
It is intentional that Hooks work inside forwardRef and memo. |
Thanks |
Time to party 💃 https://twitter.com/olivtassinari/status/1106224970043219970. |
facebook#15100) * test: Add test for shallow + forwardRef + hook * fix(react-test-renderer): shallow forwardRef hooks
Fixed in 16.8.5. |
…s (#15100) * test: Add test for shallow + forwardRef + hook * fix(react-test-renderer): shallow forwardRef hooks
Fixes
Invariant Violation: Hooks can only be called inside the body of a function component
when shallow rendering aforwardRef
component that uses hooks.is valid when rendering as far as I can tell. At least it's a nice shorthand compared to declaring the function component with e.g.
forwardedRef
and then using an additional render function to pass the ref toforwardedRef
.