React syntax error when adding class properties

后端 未结 2 1326
借酒劲吻你
借酒劲吻你 2020-12-21 15:42

I\'m writing a react app with babel and webpack. It\'s been going along well until I tried to add a property on a class - specifically trying to use a Dropdown from React-To

2条回答
  •  感情败类
    2020-12-21 16:13

    You can write your component like this

    class ChordFilters extends Component {
    
      constructor() {
        super();
        this.state = {
           value: 'Mandolin',
        };
    
      handleChange = (value) => {
        this.setState({value: value});
      };
    

    The state is a class instance variable. You should be using constructors for initializing such variables. Otherwise you have to use

    {
      "plugins": ["syntax-class-properties"]
    }
    

    in the .babelrc file to let babel know about properties.

提交回复
热议问题