Answer the question
In order to leave comments, you need to log in
How to update state value using React Hooks?
Hello. Decided to delve into hooks. And there was a question in front of me. If, using a class component, in order to get the value from the input, I did this:
import React, {Component} from 'react';
class Test extends React.Component {
constructor() {
super();
this.state = {
someValue : ''
};
};
handleInput = event => {
this.setState({someValue : event.target.value});
};
render() {
return (
<input type="text" onInput = {this.handleInput}/>
)
}
}
const MyPosts = props => {
const [newPost, useNewPost] = useState('');
const handleInput = event => useNewPost(event.target.value);
const addPost = () => {
console.log(newPost);
};
return (
<div>
<textarea onInput = {HandleInput}></textarea>
<p onClick = {addPost}>Добавить</p>
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question