React,ES6 - getInitialState是在纯JavaScript类上定义的

115 ecmascript-6 reactjs

我有以下组件(radioOther.jsx):

 'use strict';

 //module.exports = <-- omitted in update

   class RadioOther extends React.Component {

     // omitted in update 
     // getInitialState() {
     //    propTypes: {
     //        name: React.PropTypes.string.isRequired
     //    }
     //    return {
     //       otherChecked: false
     //   }
     // }

     componentDidUpdate(prevProps, prevState) {
         var otherRadBtn = this.refs.otherRadBtn.getDOMNode();

         if (prevState.otherChecked !== otherRadBtn.checked) {
             console.log('Other radio btn clicked.')
             this.setState({
                 otherChecked: otherRadBtn.checked,
             });
         }
     }

     onRadChange(e) {
         var input = e.target;
         this.setState({
             otherChecked: input.checked
         });
     }

     render() {
         return (
             <div>
                 <p className="form-group radio">
                     <label>
                         <input type="radio"
                                ref="otherRadBtn"
                                onChange={this.onRadChange}
                                name={this.props.name}
                                value="other"/>
                         Other
                     </label>
                     {this.state.otherChecked ?
                         (<label className="form-inline">
                             Please Specify:
                             <input
                                 placeholder="Please Specify"
                                 type="text"
                                 name="referrer_other"
                                 />
                         </label>)
                         :
                         ('')
                     }
                 </p>
             </div>
         )
     }
 };
Run Code Online (Sandbox Code Playgroud)

在使用ECMAScript6之前一切都很好,现在我收到1个错误,1个警告,我有一个后续问题:

错误:未捕获的TypeError:无法读取属性'otherChecked'的null

警告: getInitialState是在RadioOther上定义的,这是一个普通的JavaScript类.这仅适用于使用React.createClass创建的类.你的意思是改为定义一个州财产吗?


  1. 任何人都可以看到错误所在,我知道这是由于DOM中的条件语句,但显然我没有正确地声明其初始值?

  2. 我应该使getInitialState静态

  3. 如果getInitialState不正确,声明我的proptypes的适当位置在哪里?

更新:

   RadioOther.propTypes = {
       name: React.PropTypes.string,
       other: React.PropTypes.bool,
       options: React.PropTypes.array }

   module.exports = RadioOther;
Run Code Online (Sandbox Code Playgroud)

@ssorallen,这段代码:

     constructor(props) {
         this.state = {
             otherChecked: false,
         };
     }
Run Code Online (Sandbox Code Playgroud)

产生"Uncaught ReferenceError: this is not defined",而在下面纠正

     constructor(props) {
     super(props);
         this.state = {
             otherChecked: false,
         };
     }
Run Code Online (Sandbox Code Playgroud)

但现在,单击其他按钮现在会产生错误:

Uncaught TypeError: Cannot read property 'props' of undefined

Ros*_*len 238

  • getInitialState在ES6课程中不使用.而是this.state在构造函数中赋值.
  • propTypes 应该是静态类变量或分配给类,不应将其分配给组件实例.
  • 成员方法在ES6类中不是"自动绑定"的.对于用作回调的方法,可以使用类属性初始值设定项,也可以在构造函数中指定绑定实例.
export default class RadioOther extends React.Component {

  static propTypes = {
    name: React.PropTypes.string.isRequired,
  };

  constructor(props) {
    super(props);
    this.state = {
      otherChecked: false,
    };
  }

  // Class property initializer. `this` will be the instance when
  // the function is called.
  onRadChange = () => {
    ...
  };

  ...

}
Run Code Online (Sandbox Code Playgroud)

有关ES6类的React文档中的更多信息:将函数转换为类


Sud*_*han 5

添加罗斯的答案。

您还可以在 onChange 属性上使用新的ES6 箭头函数

它在功能上等同于this.onRadChange = this.onRadChange.bind(this);在构造函数中定义,但在我看来更简洁。

在您的情况下,单选按钮将如下所示。

<input type="radio"
       ref="otherRadBtn"
       onChange={(e)=> this.onRadChange(e)}
       name={this.props.name}
       value="other"/>
Run Code Online (Sandbox Code Playgroud)

更新

这种“更简洁”的方法比 @Ross Allen 的答案中提到的选项效率低,因为每次render()调用该方法时它都会生成一个新函数