score:3
The state does not update when I change the selections
This is because you are not dispatching a Redux action in your handleChange
method, only in your handleSubmit
method. Simply dispatching the appropriate action in handleChange
will resolve this issue.
when I navigate away from the page, the state that was last stored is not retained
This is because the values from before (before you navigate away) will only be kept in the Redux store, while the form fields are populated from the local state of the SearchAdvanced
component.
To solve this one well, you should get rid of your local state entirely. Instead only use the Redux store. Keeping both intact is unnecessary and breaks the 'Single Source of Truth' that Redux is meant for. I recommend you do away with the local state and only update the Redux state and then pass values to the form inputs from the Redux store (props
for the component).
Regarding your note that you tried this, but get errors: you need to change anything like:
<input value={ this.state.formValues.someValue }/>
to
<input value={ this.props.formValues.someValue }/>
where formValues
comes from the Redux store.
Update
Problem now is the line
this.props.setFormValues(this.props.formValues)
in handleChange
. You're using the old formValues to update, so the store never actually updates. I think you want something like:
handleChange(event) {
event.preventDefault();
const { name, value, type, checked } = event.target;
this.props.setFormValues({
...this.props.formValues,
[name]: type === 'checkbox' ? checked : value
});
}
So that you are updating the store's formValues
with the input from the user. The ternary operator is necessary for checkbox inputs since the value
of a checked checkbox is 'on'
rather than true
, but the checked
attribute is true
if checked.
Extra
It seems that you pass the dispatch method to the SearchAdvanced
via props from the parent. You can (and should) do this more cleanly by using the second argument of connect
, which is mapDispatchToProps
. Here's an example:
const mapDispatchToProps = {
getSearchResults,
setSearchedOnce,
setPageNum,
setFormValues
}
export default connect(mapStateToProps, mapDispatchToProps)(SearchAdvanced);
Then you can just call any of these as methods that already have a dispatch bound to them. So
this.props.dispatch(setSearchedOnce(true))
becomes
this.props.setSearchedOnce(true)
and you can remove the passing of the dispatch from the parent component.
Docs on connect
: https://github.com/reduxjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options
Source: stackoverflow.com
Related Query
- React Redux - How to store form state so that it can be populated again when user returns to page
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- React Redux - How do I build a form with material-ui that I can add inputs to?
- When using React / Redux, how is dispatching an action that merely changes the Redux state vs actually doing some task?
- How to use React Redux store in component that modifies and renders the state
- How we can maintain the state of logged in and loggedout users and share that state to homepage in react redux
- How to store user logged in state with React Redux
- i need to access user info which are initially store in redux store when logged in.. How can i do that? React-redux
- How to display the last page when user clicks on add new row,so that user can view the newly added row using react js and prime react
- When do I choose React state Vs Redux Store
- How to reset state of Redux Store when using configureStore from @reduxjs/toolkit?
- How do I use local state along with redux store state in the same react component?
- How can I clear the localstorage when user close react application window?
- how and when to call a react component methods after state change from redux
- ReactJS - How can I implement pagination for react with keep state when route change?
- How can I spread props to a React component that uses exact props when using Flow?
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- How can I initialize Redux state for React component on its creation?
- How can I store and subscribe component state to redux store?
- React Redux Toolkit: How can I can store my initialState to localStorage using createSlice?
- How to update React UI from with state in Redux store with useEffect
- How React Testing Library can assert children props and Redux store updates?
- How to re-render react component when mapping over state that is array of objects
- How can I update state in Redux - moving todo up and down - React
- How can I watch for state of data in redux container, when I want to sort or filter it?
- react / redux toolkit useSelector not updating when store state changed
- How can I redirect to another component in react and pass the state that I set in the previous component?
- How can I affect the state of just one individual component(card) when i have multiple versions of that component?
- How would I store text in a state that the user wrote using Redux?
- How remember that a React hooks component is unmounted, so can avoid state changes?
More Query from same tag
- Can't update the font size in react using tailwindcss library
- Redux thunk: wait for async function to dispatch
- React Spring translate animation not working and clicking on a list item seems to be delayed
- Highcharts 3D render problem on setState()
- Axios does not send appropriate error response [React]
- How to render a foreach with ReactJS in Laravel
- Forcing a react functional component to trigger a rerender
- Why do flow-typed libdefs types not work consistently?
- How to storeimage from input in mobx-state-tree model?
- Accessing all form values from field validate function in React-Final-Form
- How to get Field value and pass it to FieldArray value with Push event? Redux-Form V6-RC3
- Adding a class after completing the action
- Typescript interface extends React.FC
- unexpected use of event when i pass another thing with it in parameter
- useEffect cleanup function
- Change active state in a list using useState
- How to use a parameter on the url with react-router-dom and query string and use it?
- Is there a css selector to take into account parent class?
- How to redirect to the authentication login the jwt token in react?
- Table head keeps displaying it's content instead of just once on fetch
- Running event handler once
- My Google Maps is not taking my footer CSS
- Access array of objects to display in React Native Text component
- React - Handling Null values passed as props
- Testing component with custom hook
- Jest not honoring mocking of imported modules
- PayPal .Net transfers from a customer to another customer
- Remove element from array inside React's return method
- Disable Debugging in Expo For React Native App
- Custom permissions for different user types django rest framework