I'm building an app in react and one facet of it involves recording the activity from a textarea and displaying it in real time at a different place in the app. I have the textarea bound to an onChange event and the onChange is firing, but the problem I'm having is that the onChange function keeps recording the inputs one keystroke too late. For example, if I typed "hello" in the textarea, the onChange listener would record and output "hell".
Code is below. thanks for your help!
The component code:
export default class StepTwo extends React.Component {
constructor(props) {
super(props);
this.state={
headlineText: '',
}
this.writeHeadlineChild = this.writeHeadlineChild.bind(this);
}
updateHeadlineValue(evt) {
this.setState({
headlineText: evt.target.value
})
}
writeHeadlineChild(e) {
e.preventDefault();
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
this.props.headWriter(this.state.headlineText, this.state.headlineFont);
}
render() {
return (
<
form className = "card-form"
onChange = {
(e) => {
this.writeHeadlineChild(e)
}
} >
<
div className = "form-group"
onChange = {
evt => this.updateHeadlineValue(evt)
} >
<
label htmlFor = "headline"
className = "form-label" > Your message < /label> <
textarea className = "form-control"
id = "headline" > < /textarea> <
/div> <
/form>
<
/div>
)
}
}
And the function in the parent component:
writeHeadline(t, f) {
var headline = document.querySelector('.headline');
function headlinePreview() {
headline.innerHTML = t;
headline.style.fontFamily = f;
}
headlinePreview();
}