I am learning React by building a front end to a remote REST API. (I can modify the remote API if needed.) So far, I have a function on the front end like this:
function FooForm() {
const [inputs, setInputs] = useState({});
const handleChange = (event) => {
const name = event.target.name;
const value = event.target.value;
setInputs(values => ({...values, [name]: value}))
}
const handleSubmit = (event) => {
event.preventDefault();
// Here is where I'd post to the API
}
return (
<form onSubmit={handleSubmit}>
<label>Foo
<input
type="text"
name="foo"
value={inputs.foo || ""}
onChange={handleChange}
/>
</label>
<br />
<label>Bar
<input
type="text"
name="bar"
value={inputs.bar || ""}
onChange={handleChange}
/>
</label>
<br />
<input type="submit" />
</form>
)
}
It will POST to an API endpoint. But how can I manipulate the form so that the text fields are pre-loaded with values from the database, e.g. something like value={inputs.foo || preload.foo}
, and so on?
This poster answered his own question by opting to use the useEffect
hook in a certain way, but it is unclear from the context how React/fetch deals with cookies. Does that happen automatically? Is there a better way?
2
Answers
A few things, the best practice is for your
input
‘svalue
to have the initial value of the stateinputs
which in your case is an object with propertiesfoo
andbar
instead of using||
operator.Then to include cookies in your HTTP request it’s nothing related to React but rather the library you are using to make those HTTP request.
Let’s say you use Fetch API and their way is to include an optional object with a key-value of:
credentials: 'include'
.And no you actually wouldn’t need
useEffect
hook in this case since you are justPOST
ing a request, theuseEffect
is required only if you were to receive the data in the same component, which I don’t see that you are doing that in your current code, but I can add some example since you mentioned that.An extra tip: if you were to use a
button
element inside yourform
it has, by default, atype="submit"
so you don’t have to explicitly code it, but it’s still a best practice to include it.