withState state updater callback function does not return value #466
Comments
As far as i know the setState callback does not return you the new state? It seems to me as if it is working correctly. The props do not reflect the changed value because the props object that you have available in the callback is the "old" props object. As the handler was created with the old props object. See this edited JS bin https://jsbin.com/nizigijipu/edit?js,console,output |
Ok, you're right, Because right now I can't find any use for a callback function - whats the use of it at this point if I don't have updated value when it fires? I need to do something with the new state after it gets updated and it seems that I can't do it with withState at the moment. |
I kind of agree that the callback function might not be very useful in the case that you want to use the new state. This sounds more like a case where you'd benefit from using As the React docs mention:
|
Thanks for explenation. This is a valid point, yet I would prefer not to include another HOC like |
@plakak in your case, you can pass the new state to the handler function as a argument, or compute the new state and assign it to a local variable inside the handler function. |
@wuct yes, this is how I got around this - by local variable. But callback would be better in my opinion for such async change. Right now I don't really see how it could be useful if I'm not sure my state got updated when it's called. |
Dose React pass the new state to setState second callback? |
No, it does not. But the callback is executed after the new state is set & component re-rendered so doing |
@plakak I got your point and I agree with you. It is better to pass the new state in our case. Would you like to create a PR for it? |
@wuct Sure, I'll look into it this weekend. |
Just question from my side. |
@istarkov Sorry for a late reply - I've been busy lately. In my project, I have few components, each of them have For me, it would be ideal that after filters state gets updated I can check if I have some filters or not and update my global state. Yes, of course, I can add lifecycle method to listen on every change but the reason why I use recompose is to minimize the amount of code and overhead. If I need state and lifecycle method from recompose, just to check state change, I can write a class instead. In my opinion, if I have a HOC that provides me with a state I should be able to get that new updated state there and not to rely on other HOCs just to support this one. But that's maybe just me :) |
So there is many |
@wuct If two or more components would benefit from using a given state then I put it into Redux. My other components won't and shouldn't care about The outcome is still the same for my app; something is active or not. |
@plakak Sometimes you need redux not only to share state between components, |
PS: For now I hosts some reducers in the same folder as container, like in popular react boilerplate here https://github.com/react-boilerplate/react-boilerplate/tree/master/app/containers/HomePage And having that Container specialized reducer is hosted with Container itself, it make me feel that it's not some kind of global state, but the state needed for this Container even it isn't shareable. |
In my mind I always think about any Component which has internal state will it work or not if it will be placed inside some kind of |
@istarkov Maybe I'm misunderstanding your point, but my component doesn't depend on external props like in the example(#368) But even if you have something different in mind and I would put |
To be clear after reading your comments what I understand: |
If yes now about harm:
So at least for one render global state could be inconsistent with local state. May be in your case you don't use global state at all etc no problem. But in general it can be a problem. |
Maybe I'm not doing it right, but based on the documentation I am able to provide a callback function to state updater that is executed after component re-render.
Both forms accept an optional second parameter, a callback function that will be executed once setState() is completed and the component is re-rendered.
I would assume that I will get the new state value (just like with setState's callback). Or that props would be already changed to reflect new state since it's called after re-render.
I can't make it work; here's a JS bin with the issue: https://jsbin.com/pobonu/edit?js,console,output
Is my implementation/assumption wrong or something's wrong with recompose?
The text was updated successfully, but these errors were encountered: