score:1
Accepted answer
You can use watch
to keep track of the field value in the render lifecycle and setValue
to update the form value imperatively:
const { register, watch, handleSubmit, setValue } = useForm({
defaultValues: {
quantity: 0
}
});
const quantity = watch("quantity");
const increaseQuantity = () => setValue("quantity", quantity + 1);
const decreaseQuantity = () => {
if (quantity > 1) {
setValue("quantity", quantity - 1);
}
};
useEffect(() => {
console.log(quantity);
}, [quantity]);
return (
<form onSubmit={handleSubmit((data) => alert(JSON.stringify(data)))}>
<button type="button" onClick={decreaseQuantity}>
-
</button>
<button type="button" onClick={increaseQuantity}>
+
</button>
<input {...register("quantity")} id="quantity" type="number" />
<input type="submit" />
</form>
);
References
Source: stackoverflow.com
Related Query
- Uploading a file using only the input field - React Hook Form
- Support callback for changing another field value when using React Hook Form validation
- React Hook Form and Material ui component-inputRef not working-A component is changing an uncontrolled input of type
- Implement a clear button for a custom input field to trigger required error with react hook form
- Clear form input field values after submitting in react js with ant-design
- React form error changing a controlled input of type text to be uncontrolled
- React hook form - Field Array inside Dialog (Material UI)
- React hook form - Register field not working
- React - automatic submission of form upon changing input
- Get React to display individual error messages under form input field
- Uncontrolled Input to Controlled Input Warning in React Hook Form and Material UI's TextField
- Unable to clear the input field after submitting the form in React
- Changing the value of input field - React
- React hook changing the state variable of wrong component when file input
- React hook form useFieldArray typescript error on input name
- Changing the value of nested field in react final form
- React Hook Form Register Different Forms With Same Field Names
- React hook form does not work with input from reactstrap
- File input validation using React Hook Form and Yup
- How can I validate array field in React Hook Form with the useFieldArray hook?
- Switching input field form validation in react
- Custom react hook to set input form not working
- How to input an existing value into an input field in React for an edit form
- How to validate select input field using React Hook Form?
- React phone input 2 with react hook form
- React input field doesn't see value changing
- Input field value is not updating in React form
- Why does React Hook Form think field not filled in with Chakra UI?
- Record a changing input field in react-hook-form?
- Child component's input field not changing with change in parent's props react
More Query from same tag
- D3 Selection always returns all items after enter() regardless of whether they are new or not
- React Spinner Loading Animation / View without State?
- How can I implement right swipe off functionality in a material-UI Snackbar component?
- JSX element type 'x' does not have any construct or call signatures
- Fetch url to React component
- How can I access the original names of my input fields, in dot notation, from formik context?
- How to render the array of data in dropdown list
- How to get all selected values of Tag Picker in Fluent UI
- Problem in navigation bar as Im not able to make it responsive
- LogoutRequest has no PostLogoutRedirectUri in it when using React.js app with ... library
- How to use this.executeAction("someAction") in function based Component?
- How to configure node live server? I'm getting rollbackFailedOptionalerror
- On updating @testing-library/jest-dom version from 4.0.0 to 4.2.0 react test case is failing
- Jest - mocking a node module that requires prior configuration
- Reactjs: CJSX Nested Conditionals
- Show SnackBar Material UI when appear erron in Mutation
- Unable to get Plotly.js graph to resize within react-grid-layout
- Calling the second function after calling the first function and fetching the data, react + redux
- Remove a nested element from state with $splice - React
- Concurrent issue in React.JS
- Cannot update object value, keeps overwriting by strange id | javaScript Mongodb
- TypeScript: make field public only for special classes
- How to create a lambda function to update a new record on amplify?
- Attempting to lift React state while maintaining access to a variable calculated lower down
- react router link changing url but not the component
- How to set input value to null in react
- Listen to 'invalid' event on a React component
- React - show classname only if variable true
- React input value changing from defined to undefined after validation + submission
- Two way binding on React onChange is not working