Closed augustuswm closed 5 years ago
Curious: Why exactly are you nesting useSelect
calls like that? You're not even using s1
in your example code.
Yeah, I should have noted, this example is pretty contrived to reduce it down. The actual implementations are more along the lines of nested hooks. Something like:
useFeaturedImage() {
return useSelect(select => {
return // .. use select to get featured image ...
}
}
usePost() {
return useSelect(select => {
let image = useFeaturedImage();
// ... use select to retrieve some other data...
})
}
A number of smaller hooks that get composed.
Edit: Actually I think the use case here is invalid and probably violates the Hooks rule of not calling hooks in conditionals / loops / nested functions.
While working on a hook library we ran into an issue that looks to stem from interactions between nested useSelect calls. I've tried to create a reduced example to trigger the error. Unsure if this is an error on our part with regards to usage of the hook / select, or a bug elsewhere.
To reproduce With
SCRIPT_DEBUG
set to true. Use the following to create a custom block and add it to a post. Save a draft of the post, and reload. React should report a warning of:and an error of:
Test Hook:
Test Block:
Expected behavior This block should display an empty div or a div with the id of the featured media when one is assigned.
Environment Chrome 77.0.3865.90 macOS 10.14.5 WordPress 5.3-beta2-46382 Twenty Twenty (twentytwenty) 1.0