react父子组件通信

父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据。



class TemperatureShow extends React.Component {
   constructor(props) {
       super(props);
   }
   render() {
       let t = this.props.temperature;
       if(t > 38) {
           return 

hot

; }else if(t <= 38 && t >= 20) { return

cool

; }else { return

cold

; } } } class TemperatureInput extends React.Component { constructor(props) { super(props); // this.handleTemp=this.handleTemp.bind(this); } handleTemp(e) { this.props.onTemperatureChange(e.target.value); } render() { return (

); } } class TemContainer extends React.Component { constructor(props) { super(props); this.state = { temperature: '', }; this.handleTemp = this.handleTemp.bind(this); } handleTemp(temperature) { this.setState({ temperature: temperature, }); } render() { let temperature = this.state.temperature; return (
); } } ReactDOM.render( , document.getElementById('root'), );

你可能感兴趣的:(react父子组件通信)