-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
How to clear/remove state properties? #962
Comments
Yes, the
That's what Of course, whether deleting the |
Got it. That makes sense. Thanks a lot. |
My google search pointed me here and the answer above seemed vague. How about the following as implementation to removing keys... It filters the key that should be deleted then builds a new object from the remaining keys and the initial object. The idea is stolen from Tyler McGinnes awesome reactjs program.
|
Another solution would be
|
Is there something wrong with:
|
@Halt001 If you change |
You can use destructuring to remove the properties. Here is a function that I use in my validation reducers to return a valid field state after making sure that the validation passed. It removes the function validFieldState(fieldState) {
if (fieldState.invalid) {
const {
invalid: _invalid, // <-- The `_invalid` and `_error` variables are never used.
error: _error,
...nextFieldState, // <-- This variable gets every prop except for `invalid` and `error`.
} = fieldState;
return nextFieldState;
}
return fieldState;
} This is a common pattern used in React, to remove certain |
The only headache with your approach @waynebloss, is that a lot of linters do not allow unused variables. You can always disable a rule temporarily, but that kind of inconsistency is not ideal. Especially in a production deployment. Are there any other approaches besides the ones mentioned here? |
@prufrock123 I should have mentioned that the variables should be prefixed with an underscore as shown in my example, because the linter that is built into create-react-app will automatically ignore any unused variable that starts with an underscore. This seems to be a common feature of other linters and I see it mentioned a few different places: Couldn't find the official docs for tslint, but it should work. Which linter are you using? |
this code cleans all state variables
|
I don't think that there's anything wrong with that method. |
Since the introduction of destructuring and rest syntax to JavaScript, this can now be accomplished in another way: const { data, ...rest } = state;
return {
...rest,
loading: false,
loaded: false
} |
Still learning React JS and Redux, and found this boilerplate to be extremely helpful. Good job Erik!
I'm wondering how to clear/remove a state property, say, state.data. Note that I don't want to just set state.data to null. I want to remove state.data property all together. The following code works for me.
However, my understanding is that state in Redux should not be mutated. Although it's working, doesn't it violate the immutable rule of Redux? Actually, I don't see any code in the example making copies of state object before changing it, like most other Redux implementation does (see below).
So how does the example code get away from the whole Object.assign thing? I must be missing something. Thanks.
The text was updated successfully, but these errors were encountered: