I am learning React and came across this situation. Basically I have a React component Test with a input box and inside it I have another component Hello which print out Hello + all the text user types in . So what I expect is if i type sth then this.text will be updated and then Hello component will re-render. However this is not happening. I realize that I need to set the "text" in this.state then Hello will re-render. My question is why the Hello component is not re-render although its props is changed ? Thanks in advance.
import ReactDOM from "react-dom";
import React, { Component } from "react";
import Hello from "./hello";
class Test extends Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.text = e.target.value;
console.log(this.text);
}
render() {
return (
<div>
<h1>Test</h1>
<form>
<input value={this.text} onChange={e => this.handleChange(e)} />
<button>Add</button>
</form>
<Hello name={this.text} />
</div>
);
}
}
and the Hello.js component is
export default ({ name }) => <h1>Hello {name}!</h1>;