How to submit data from Formik in React? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to submit data from Formik in React?

Hi everyone, I work on project which has Survey page, part of the site has checkboxes with answers to the question next to them and textarea below that. Now the idea is that when the user fills out the form and clicks Submit button, I need that data to get a printout in the console.log? Here is my SurveyPage.js and CustomCheckbox.js components: https://pastebin.com/M5ZrZ5q1 https://pastebin.com/s4zXbXwt

20th Dec 2021, 10:14 AM
Filip
Filip - avatar
11 Answers
+ 1
Form usually has onSumbit. Assign an attribute to the button inside a form of type=submit. And onSubmit={submitHandler}. cont submitHandler = () => console.log(answer)
20th Dec 2021, 12:40 PM
Zubae
Zubae - avatar
0
Tnx man a lot. I set it up that way but I only get one answer per click even though I check more than one? e.g. print in console.log: {firstQ: 'Answer number 3'} I checked one, two and three but it print only last one.
20th Dec 2021, 1:12 PM
Filip
Filip - avatar
0
onClick={() => { setChecked(!checked); setAnswer ( {firstQ: label}) }} /> Over here, what is happening is the previous state is being overwritten everytime onClick is called. Try using spread operator, to spread previousState and then add next state . For eg: onClick={() => { setChecked(!checked); setAnswer ( ...answer, {firstQ: label}) }} /> Something like this.
20th Dec 2021, 1:21 PM
Zubae
Zubae - avatar
0
onClick={() => { setChecked(!checked); setAnswer(...answer, {firstQ: label}) }} it tell me how ...answer is not defined.
20th Dec 2021, 2:48 PM
Filip
Filip - avatar
0
Pass answer as a prop, as you pass setAnswer.
20th Dec 2021, 3:03 PM
Zubae
Zubae - avatar
0
page is render but when click on checkbox, get this screen: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) 22 | name="survey" 23 | onClick={() => { 24 | setChecked(!checked); > 25 | setAnswer(...answer, {firstQ: label}) | ^ 26 | }} 27 | /> 28 | </span>
20th Dec 2021, 3:07 PM
Filip
Filip - avatar
0
return <CustomCheck key={index} setAnswer={setAnswer} answer={answer} label={label} /> In the SurveyPage name="survey" onClick={() => { setChecked(!checked); setAnswer(...props.answer, {firstQ: label}) }} /> </span>
20th Dec 2021, 3:11 PM
Zubae
Zubae - avatar
0
now it said that "props" is not defined, I forwarded like this : const CustomCheck = ({label, setAnswer, answer}) => { but on this line, props is not defined: setAnswer(...props.answer, {firstQ: label})
20th Dec 2021, 3:22 PM
Filip
Filip - avatar
0
Ah, then just pass answer, no need props.answer. const CustomCheck = ({label, setAnswer, answer}) => { setAnswer(...answer, {firstQ: label})
20th Dec 2021, 3:28 PM
Zubae
Zubae - avatar
0
I do so and then click on checkbox I get error from above that I have already sent: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
20th Dec 2021, 3:59 PM
Filip
Filip - avatar
0
this is what the files look like now: https://pastebin.com/7AYxnwdM - SurveyPage https://pastebin.com/s6GKq0k9 - CustomCheckbox
20th Dec 2021, 4:06 PM
Filip
Filip - avatar