使用reactjs从内部组件获取输入值

时间:2016-10-24 22:58:58

标签: javascript reactjs

我试图获取内部输入元素的值,但每次出现此错误时都会:

  

未捕获的TypeError:this.containerInput.gatValue不是函数

是什么

class Input extends Component {
    getValue() {
    return this.textInput.value;
  }
  render() {
    return (
      <input type="text" ref={(input) => this.textInput = input} />
    );
  }
}

class Container extends Component {
    getValue() {
    return this.containerInput.gatValue();
  }
  render() {
    return (
      <Input ref={(input) => this.containerInput = input} />
    );
  }
}

class View extends Component {
    constructor(props) {
    super(props);
    this.buttonClick = this.buttonClick.bind(this);
    }
    buttonClick(e) {
    console.log(this.viewInput.getValue());
  }
  render() {
    return (
      <div>
        <Container ref={(input) => this.viewInput = input} />
        <button onClick={this.buttonClick}>Get Value</button>
        <span></span>
      </div>
    );
  }
}

ReactDOM.render(
  <View />,
  document.getElementById('root')
);

DEMO

1 个答案:

答案 0 :(得分:2)

您的意思是this.containerInput.getValue()还是this.containerInput.textInput.value?