React 方法

React组件内自己所有的方法都要在constructor()中加一句

this.fn=this.fn.bind(this)

子组件修改父元素的数据方法

(但是个人感觉这样子违背了单一数据来源的原则),子组件都可以改变父组件的数据,会不会让数据的来源以及调试变得难以跟踪?


class TemperatureInput extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
     this.props.onTemperatureChange(e.target.value);
  }

  render() {
    const temperature = this.props.temperature;
    const scale = this.props.scale;
    return (
      
Enter temperature in {scaleNames[scale]}:
); } }

handleChange(e) {
this.props.onTemperatureChange(e.target.value);
}

你可能感兴趣的:(React 方法)