-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
feature request: getState, example, const [state, setState, getState] = useState(null); #21931
Comments
I agree that this would be nice indeed. At the moment, when I need to be able to retrieve dynamically the current state, I'm doing things like: const [state, setState] = useState();
const stateRef = useRef();
stateRef.current = state;
...
// in a callback somewhere:
const currentState = stateRef.current which isn't safe for concurrent mode, but setting the ref in And sometimes, I'm also doing: const [state, setState] = useState();
...
// in a callback somewhere:
setState(currentState => {
// here I have the current value of the state
return setState;
}); but this is kinda weird and breaks the flow of the code (and can trigger re-renders the 1st time it gets called) |
@shimondoodkin shouldn't this issue be moved to https://github.com/reactjs/rfcs? |
@Ayc0 I don't know to answer, nither I think I am suting to implement this. |
Btw, I saw this twitter thread explaining the reasons behind why getState isn't returned by useState / useReducer: |
about the Twitter thread, there was no significant reasoning against it. just associated thoughts, summary, and my arguments: there were reasons like 'use reducers instead'. I think they are not relevant because it is possible to put the value somewhere else anyway, the issue is more about convenience, expressiveness, flexibility. it said there, there is no promise of in-order execution for a getState method, however, it is not expected from it. |
@shimondoodkin @Ayc0 check my proposal that was closed by @bvaughn because was duplicated the suggestion about |
@Ayc0 was right. Changes like this should go through our RFC process: The RFC process is a great opportunity to get more eyeballs on your proposal before it becomes a part of a released version of React. Quite often, even proposals that seem "obvious" can be significantly improved once a wider group of interested people have a chance to weigh in. The RFC process can also be helpful to encourage discussions about a proposed feature as it is being designed, and incorporate important constraints into the design while it's easier to change, before the design has been fully implemented. |
@bvaughn quite similar to this reactjs/rfcs#201 just that he called useStateRef |
Agreed. It looks similar. Let's actually close this issue and roll it into that RFC. (Sorry to keep moving things around. This sort of proposal really should have been an RFC to begin with.) |
feature request: getState, example, const [state, setState, getState] = useState(null);
it is ok to use the simple variable in a dependent uses effect, but not ok in not a dependant use effect.
the state is a simple variable. which is closure passed into functions, so it is not updated.
and setState with a function argument is asynchronously dispatched.
I suggest adding a synchronous function getState.
it will be useful in simple js functions that are defined in useEffect for integration with not react js
but the working implementation is like
i wish there was a synchronous getState.
The text was updated successfully, but these errors were encountered: