'constructor' 和 'super' 上的 React vs Redux 道具

cla*_*opb 5 reactjs redux

为什么我在 ReactJS 中将“props”作为参数传递,而在使用 ReduxJS 时却没有发生?

例如:

反应应用程序:

 constructor(props){
    super(props);
    this.state = {
      memeLimit: 10
    }
 }
Run Code Online (Sandbox Code Playgroud)

Redux 应用程序:

constructor(){
    super();
    this.state = {
      memeLimit: 10
    }
 }
Run Code Online (Sandbox Code Playgroud)

谢谢

Ana*_*nas 5

使用的原因super(props);是为了能this.props在构造函数里面使用,否则就不需要super(props);

例如

 constructor(props){
    super(props);
    this.state = {
      memeLimit: this.props.memeLimit // Here you can use this.props
    }
 }
Run Code Online (Sandbox Code Playgroud)

这相当于

 constructor(props){
    super();
    this.state = {
      memeLimit: props.memeLimit // Here you cannot use this.props
    }
 }
Run Code Online (Sandbox Code Playgroud)

这与 Redux Vs React 没有真正的关系

您可以查看更多详细信息:使用 es6 类时,React 中的“super()”和“super(props)”之间有什么区别?