I need to dispatch an action with the form content. The action has some async component handled by my middleware The action works fine outside of react, when setting the initial data.
The action works fine if I use it inside an useEffect() with no dependencies so it will run straight away... the store gets updated ad the api calls work.
import { React, useState, useEffect } from "react"
import { useSelector, useDispatch } from "react-redux"
import { apiCallBegan } from "../store/api"
const NewQuestionForm = () => {
// const [optionOneText, setOptionOneText] = useState("")
// const [optionTwoText, setOptionTwoText] = useState("")
const [submitted, setSubmitted] = useState(false);
// const author = useSelector(state => state.auth)
const dispatch = useDispatch()
useEffect(()=>{
dispatch(apiCallBegan({
callMethodName: '_saveQuestion',
data: { optionOneText: "I believe I can fly", optionTwoText: "I donte believe I can Fly", author:'helenfoneing' },
onSuccess: ['questions/questionAdded','users/questionAdded'],
onError: 'apiRequestfailed'
}
))
},[])
return (
<form className="new-question-form"
// ... rest of component
</form>)
}
but if I try to trigger form action upon submission of form , the whole app gets reset, just like pressing F5 on the browser:
/* eslint-disable no-unused-vars */
/* eslint-disable react/prop-types */ // TODO: upgrade to latest eslint tooling
import { React, useState, useEffect } from "react"
import { useSelector, useDispatch } from "react-redux"
import { apiCallBegan } from "../store/api"
const NewQuestionForm = () => {
const [optionOneText, setOptionOneText] = useState("")
const [optionTwoText, setOptionTwoText] = useState("")
const [submitted, setSubmitted] = useState(false);
const author = useSelector(state => state.auth)
const dispatch = useDispatch()
useEffect(()=>{
submitted && dispatch(apiCallBegan({
callMethodName: '_saveQuestion',
data: { optionOneText: "I believe I can fly", optionTwoText: "I donte believe I can Fly", author:'helenfoneing' },
onSuccess: ['questions/questionAdded','users/questionAdded'],
onError: 'apiRequestfailed'
}
))
},[submitted])
return (
<form className="new-question-form"
onSubmit={(e)=>{
setSubmitted(true)
e.preventDefault}}
>
NEW QUESTION FORM HERE
<label>1.
<input type="text"
value={optionOneText}
onChange={(e) => setOptionOneText(e.target.value)} />
</label>
<label>2.
<input type="text"
value={optionTwoText}
onChange={(e) => setOptionTwoText(e.target.value)} />
</label>
<input type="submit" />
</form>)
}
export default NewQuestionForm
I am not sure what the problem is. The action data is hardwire to isolate the problem