<select className="form-control"
placeholder="Category name"
name="category"
value={course.category}
onChange={handleChange}>
{categories.map((category) => <option key={category._id} data-id={category._id}>{category.categoryName}</option>)}
</select>
Setting default value:
const handleCourseSubmit = async e => {
e.preventDefault()
setCourse((prevState) => {
return { ...prevState, category: categories[0].categoryName }
})
console.log('create.js:: handleCourseSubmit:: data: ', course.category); //prints empty string created from initial state
}
How do I set default value on submit?
2
Answers
You can use the useEffect hook.