score:1
I put your code in a code sandbox and it seems to work just fine. Note that i removed className
properties and commented stuff that is not necessary to test your issue like redux
imports.
Since I removed the className
properties this could be an CSS
issue, where your error span
is actually rendered but not visible (check out the dev tools of your browser to see if the span
is really not there).
Also I would recommend you to use a library if you use a lot of forms, since state handling + validation could get quite complicated and there are plenty solutions out there.
I wrote my own library - react-fluent-form - feel free to check that out.
EDIT
The issue here is that when you update the error
object using setError
you always pass the same object reference:
// this is not doing a copy
// validationError will have the same reference as error
let validationError = error;
// ...
// following line will not trigger a rerender
setError(validationError);
Since error
and validationError
have the same reference, react
will asume no change has happened, thus it will cause bail out of a state update. If you work with complex types (like objects or arrays) in state you always need to create a new reference instead of adapting the previous one:
// this is an actual copy using the spread operator
// validationError will have different reference than error
let validationError = {...error};
// ...
// triggers rerender as expected
setError(validationError);
EDIT 2
I added a return value for validate
to use the updated validationError
object when calling validateForm
.
const validate = () => {
//..
let validationError = { ...error };
// ...
return validationError;
};
const onSubmit = e => {
// ...
const validationError = validate();
if (validateForm(validationError)) {
//...
}
};
See the updated code sandbox.
Source: stackoverflow.com
Related Query
- While doing form validation on submit a form, the component is not updating in React
- 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 useState not updating the states on submit form
- React not updating data on parent component after child component form submit
- Component not updating when I change the props that I pass to it in React
- Formik React with 2 buttons (Submit and Save) to submit form - Save button not to trigger validation
- The validation errors are not showing using React Hook Form
- React router 4 `Link` component only changing the url and not updating the route
- component not re-rendering when updating the state in mobx
- The code is working fine in codeSandbox, But showing error while doing in the IDE as "can't define property "email": Object is not extensible"
- Why is the child component updating but not re-render Reactjs
- Why is the child component not updating with state of parent?
- State not updating until 2nd time the form is submitted
- Can Ant Design form validation be skipped upon clicking the submit button?
- onChange the textbox value is not updating in HTML form using React
- Value of Form Input not updating on Submit in React
- React custom hook with event listener not works while updating the state
- redux-form: path.lastIndexOf is not a function error thrown while unmounting component due to "Object object" as form name
- Is there a way to set defaultValues in a child component using react hook form with useFieldArray hook - not using the useForm hook?
- React loadStripe method not updating the element form on country wise selection
- Select option child component not reseting to key 0 on form submit with reactjs
- redux Form 7.4.2 Server side validation not working with axios , gives error Unhandled Rejection (SubmissionError): Submit Validation Failed
- React form not displaying the correct inputs on the screen after I click submit button
- React functional component is not rendering when updating the Sate after pushing data to array
- Completely stumped at this reactjs logic, of a variable not updating the state of a component
- Component responsible to display all my transactions are not updating after submit
- Clicking the submit button to go to the next tab only changes the URL but not the tab and the component itself. How can I fix?
- React Hook form validation does not work on a controlled component
- Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry. while using wrapper.html()
- Why is the DOM not reflecting my component state in my dynamic form
More Query from same tag
- Automatically loading externals with Webpack
- How to change the route according to the value of a field
- Project Test failing due to Reference Error - React
- How to render array of object?
- How to organize types definitions in a React Project w/ Typescript
- Not Able to get request body as desired
- material-ui 4.3.2 context menu mouse right click position
- Testing a login component with Jest
- Hide item from list on click
- React use useEffect to load data asynchronously
- TypeScript error while accessing Proxy Object | Object[] in MobX
- Attempted import error: 'useRouteMatch' is not exported from 'react-router-dom'
- Removing event listener in React (lodash.throttle)
- Passing entire props to auxiliar functions triggers eslint prop-type errors
- How to use the callback function in ReactJS Hooks
- React server not starting - something with renderProps
- How to store token in react
- How to use jinja2 server side rendering alongside react without violating inline-script CSP
- How to change state value when input is checked with useState hook
- How to send image from image Uri through HTTP request? (React Native and Django Backend)
- Error in my backend when I'm trying to send an array of ObjectID from a post router from my frontend (React-Native/Mongoose)
- Steps to populate dynamic Dropdown using arrays in REACTJS using react hooks
- onChange and onSelect are not working for Select inside a loop in React Hooks
- Reagent input not updating
- Dynamically generating components in React-Bootstrap
- Why ref='string' is "legacy"?
- How to specify / select a particular property of an object
- Inline background-image in React
- Manually refresh or re-render component onClick in React
- NextJS: How to correctly add firebase analytics to NextJS app