-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conditional useSelect not working as expected #47145
Comments
The const value = useSelect( ( select ) =>
!! link ? select( STORE_NAME ).getValue() : '',
[ link ]
); |
Thanks for the tip, @TimothyBJacobs. It looks like, indeed, the dependency array fixes the issue. I was just surprised that this hook doesn't work: useValue = ( condition ) => useSelect( ( select ) =>
!! condition ? select( STORE ).getValue() : ''
); but this one does: useValue = ( condition ) => useSelect( ( select ) => {
const { getValue } = select( STORE );
return !! condition ? getValue() : '';
} ); as if the mere act of calling the If that were the case, I think it'd be a good idea if we could explicitly mention this in the documentation. What do you think? |
Related to the discussion we had in the useSelect refactoring PR cc @jsnajdr @kevin940726 |
I agree that we should either fix this as a bug or mention in the documentation that conditional selects are not supported. |
Let's fix this as a bug -- it shouldn't be terribly hard or expensive to compare the list of selected stores after each call. Especially with the new |
Proposing a fix in #47243. |
Description
useSelect
doesn't re-render a component after the store is updated ifuseSelect
's callback uses theselect
argument conditionally. In other words...This hook doesn't work:
but this one does:
Step-by-step reproduction instructions
useSelect
logic:(This component lets you write some text. It extracts the first link and saves it in the store with a
useEffect
).TextControl
: Hello, World. https://example.comExpected behavior: You should see
"https://example.com"
in thepre
tagActual behavior: You see the empty string
""
, even thoughwp.data.select( STORE_NAME ).getValue()
returns the proper value https://example.com.Screenshots, screen recording, code snippet
No response
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: