When i write in the input element, it triggers onInputChange() function, that updates inputValue state, then calls the getValue() that gets the inputValue state and log in console. The value that is rendered is not the same that is in console, what's happening here?
Reproducible example: https://stackblitz.com/edit/react-i4wprk?file=src%2FApp.js
import React from 'react';
import './style.css';
export default class App extends React.Component {
constructor() {
super();
this.state = {
inputValue: '',
};
}
getValue = () => {
const { inputValue } = this.state;
console.log(inputValue);
};
onInputChange = (event) => {
const inputValue = event.currentTarget.value;
this.setState({ inputValue });
this.getValue();
};
render() {
const { inputValue } = this.state;
return (
<div>
<input placeholder="texto" onInput={this.onInputChange} />
<p>{inputValue}</p>
</div>
);
}
}
this.setStateis asynchronous so when you do it and right after callthis.getValue(), the data is not already set in the statethis.setState({ inputValue }, () => this.getValue());