为什么在构造函数之外设置React Component的状态? [英] Why set a React Component's state outside of the constructor?

查看:152
本文介绍了为什么在构造函数之外设置React Component的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚刚从React框架下载了源代码,我在终端中收到了这个错误:

So I just downloaded source code from a React framework, and I'm getting this error in Terminal:

  ERROR in ./src/components/TextEditor.js
  Module build failed: SyntaxError: Unexpected token (24:8)

  22 | 
  23 |   // Set the initial state when the app is first constructed.
> 24 |   state = {
     |         ^
  25 |     state: initialState
  26 |   }
  27 | 

我的问题是,为什么人们会像这样设置React Component的状态?如果某些人的错误会有什么好处?另外,我可以使用Babel预设或插件来防止此错误吗?

My question is, why do people set a React Component's state like this? What's the benefit if it'll error for some people? Also, is there a Babel preset or plugin I can get to prevent this error?

这就是我通常设置组件状态的方式,从我看到的情况来看,这是传统的:

This is how I usually set a component's state, and from what I've seen, this is conventional:

constructor() {
  super();
  this.state = {
    state: initialState
  };
}

记录中,这是整个文件:

For the record, this is the entire document:

// Import React!
import React from 'react'
import {Editor, Raw} from 'slate'

const initialState = Raw.deserialize({
  nodes: [
    {
      kind: 'block',
      type: 'paragraph',
      nodes: [
        {
          kind: 'text',
          text: 'A line of text in a paragraph.'
        }
      ]
    }
  ]
}, { terse: true })

// Define our app...
export default class TextEditor extends React.Component {

  // Set the initial state when the app is first constructed.
  state = {
    state: initialState
  }

  // On change, update the app's React state with the new editor state.
  render() {
    return (
      <Editor
        state={this.state.state}
        onChange={state => this.setState({ state })}
      />
    )
  }

}


推荐答案

第一个例子是使用不属于ES6规范的类属性。您可以使用 stage-2 预设或 babel-plugin-transform-class-properties 插件与babel一起使用它们模块。

The first example is using class properties which is not part of the ES6 spec. You can use them with babel using the stage-2 preset or the babel-plugin-transform-class-properties plugin module.

这个用法主要是一个偏好的问题,并且在使用babel进行转换时会产生与第二个例子相同的结果。

The usage is mostly a matter of preference and will produce the same result as your second example when transpiling with babel.

https://babeljs.io/repl/#?evaluate=true&lineWrap=false&presets=react%2Cstage-0&experimental=false&loose=false&spec=false& ;代码=%2F%2F%20Code%20goes%20here%0Aclass%20First%20%7B%0A%20%20state%20 %3D%20%7B%0A%20%20%20%20value%3A%20true%0A%20%20%7D%0A%7D%3B%0A%0Aclass%放映20秒%20%7B%0A%20%20constructor %20()%20%7B%0A%20%20%20%20this.state%20%3D%20%7B%0A%20%20%20%20%20%20value%3A%20true%0A%20 %20%20%20%7D%3B%0A%20%20%7D%0A%7D%3B%0A

https://babeljs.io/repl/#?evaluate=true&lineWrap=false&presets=react%2Cstage-0&experimental=false&loose=false&spec=false&code=%2F%2F%20Code%20goes%20here%0Aclass%20First%20%7B%0A%20%20state%20%3D%20%7B%0A%20%20%20%20value%3A%20true%0A%20%20%7D%0A%7D%3B%0A%0Aclass%20Second%20%7B%0A%20%20constructor%20()%20%7B%0A%20%20%20%20this.state%20%3D%20%7B%0A%20%20%20%20%20%20value%3A%20true%0A%20%20%20%20%7D%3B%0A%20%20%7D%0A%7D%3B%0A

这篇关于为什么在构造函数之外设置React Component的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆