score:0
your code actually updates state 3 times. It's not the spread operator guilt, but the fact you are passing the same error
object state to each state update, and that error
has no errors.
This way, each subsequent state update overwrites the previous one with the outdated state error
, where it doesn't have any errors.
In these situations to reach the desired outcome you must pass a callback function to setErrors
. That callback will receive as argument the current state.
if you change each setErrors
:
setErrors({ ...errors, name: 'You must enter your name.' })
to receive a callback:
setErrors(errors => ({ ...errors, name: 'You must enter your name.' }))
You will see that your errors
state will update correctly, since the callback function receives the current state object.
Note: Obviously @CertainPerformance is a much cleaner code.
score:2
While you could use callbacks instead, eg
setErrors(errors => ({ ...errors, // ...
it looks like it would be a lot easier to call setErrors
only once:
setErrors({
// if these are the only properties,
// no need for `...errors`
name: user.name === '' ? 'You must enter your name.' : '',
email: user.email === '' ? 'You must enter a valid email.' : '',
password: user.password === '' ? 'You must enter a password.' : '',
});
If this is in a <form>
, you could also consider adding a required
attribute instead of checking all these manually.
Source: stackoverflow.com
Related Query
- useState setter functions not updating state when using spread operator and multiple if statements?
- useState setter not updating state when called in useEffect
- State not updating when using select and options
- Updating just part of state and keeping others same when using useState hook
- My useState hook is not updating itself and when i am trying to get data using filter, its not working
- State not update when using setter and setInterval in useEffect
- State not updating when using React state hook within setInterval
- Do I need to use the spread operator when using useState hook on object when updating?
- useEffect not being called and not updating state when api is fetched
- how useState really work when state change and state not change
- Why is a spread operator required when using .map() in React, but not in plain JavaScript?
- I am trying to render the names on my screen using "Rick and Morty" REST API,but nothing literally renders and my state is not updating
- useSelector and UseEffect with dispatch functions create loop due to not updating state
- useState not updating data when passing from parent function component using ref
- React Hooks: updating state using useState does not update the state immediately
- Ant design Form validation is not working and not updating input values when component is from a state that is passed from a function
- React: Looping over an object to bind useState functions is not updating the state of my application
- Correctly updating Object in React.js State without using spread operator
- State not updating when using useContext
- How to define TypeScript types when used with React useState and the previous state inside a state updating function?
- Button not executing both functions when passing state and props
- State is not updating when using React form. I am trying to use the user input to display an order confirmation in a modal
- React Context updated state not reflecting in nested functions when using Hooks
- React not updating its state when come back using react-router in Apollo Client
- React state not updated correctly when using useState hook
- Why does my React app menu open when I am only changing the state and am not using any CSS?
- Why am I not able to set my state using useState and send the state to firebase realtime database at the same time?
- Updating state array using Spread operator in React?
- Incompatible types between SVGPathElement and <path> when using spread operator React Typescript
- Frontend not updating when using useState
More Query from same tag
- Unify input value with React useState
- Unable to populate JSON object from SocketIO into front end React table
- What is this method carrying out in React.js?
- NextJS and Redux : Cannot use thunk middleware
- How to wait until navigator.notification.prompt() input has been given
- New version react router doesn't work with redux
- how to hide empty label in react-bootstrap-typeahead
- Arrow functions as class properties using Babel
- How to move selection point to the beginning of input field?
- React onChange to handle state objects
- Pass a function to a component arrived as '' Props ''. - React - Material UI
- How To Resize Image Before Uploading to Firebase Storage in reactjs
- Calling API when clicked on a Router
- How to implement fetch API by search
- Textfield is blurred after each key stroke after updating the local component state
- Correct way to use button navigation in react
- Pass props to higher-order stateless functional React component
- How to access a value of a dynamically named field in React using Redux Forms?
- How to prevent rollup from looking in parent node_modules folder
- Reactjs How to pass the props of current page into child page
- Understanding the "type" attribute in a <script> tag
- Add class to specific element in React onClick
- react-simple-image-slider doesnt work on first render
- how to count the number of times radio button is checked in react
- Redux forms nested initialValues
- Inserting HTML fragments without an element container in React
- React js can't access array useState by index, map works
- React-Apollo - render getting called before getDerivedStateFromProps
- Inserting JSON objects into mySQL database using node.js
- ReactJS: Detect which button is clicked