0

My problem is i create custom hook and link with my form hook tracing input text filed and update in state. I want after click on button submit input value in state with that custom hook and i am stuck here...


export const useForm = (initialValue) => {
    const [value, setValue] = useState(initialValue);
    return [value, e => 
    setValue({
        ...value, [e.target.name] : e.target.value
    })];
}

this is my custom hook

 <form onSubmit={updateCityName}>
            <div className="form">
                <div className="input-field">
                    <i className="material-icons prefix">location_on</i>
                    <input id="icon_prefix" type="text" name="cityName" value={value.cityName} onChange={handleChange} className="validate" />
                    <label htmlFor ="icon_prefix">City Name</label>
                </div>
                    <button className="btn waves-effect waves-light" type="submit" name="action">Submit</button>
            </div>
        </form>

this is my form and this is function

const updateCityName = e => {
    e.preventDefault();
    handleChange('what i must here type for update state???');
  }
2
  • Why do you want to update the state after submitting the form? Commented Aug 23, 2019 at 12:37
  • i must use that value and pass in url Commented Aug 23, 2019 at 13:09

1 Answer 1

1

You could do something like in this example: https://codesandbox.io/s/ecstatic-tree-2893r

You have the useForm to set and update form state. Then you have a second state for the submitted value.

I used 2 different states : first useForm to handle changes on inputs.

Then the 2nd state to keep submitted information, once the user clicked on the button. Of course in real scenario you will do something else with submitted info, like call a web service etc...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.