Answer the question
In order to leave comments, you need to log in
Why is there an error when using useState?
The essence of the task: to add text from the input to the paragraph by pressing the button.
import React, {useState} from 'react'
const Task = () => {
const [value, setValue] = useState();
const handleChange = (e) => {
setValue(()=>({value: e.target.value}));
};
const handleClick = () => {
return value;
};
return <div>
<input type='text' onChange = {handleChange} />
<input type='submit' value ='click' onClick = {handleClick} />
<p>{handleClick()}</p>
</div>
};
export default Task;
<p>{handleClick()}</p>
Uncaught Error: Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead.
Answer the question
In order to leave comments, you need to log in
const [value, setValue] = useState('');
const [text, setText] = useState('');
const handleChange = (e) => setValue(e.target.value);
const handleClick = () => setText(value);
<p>{text}</p>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question