在React中实例化组件

时间:2016-07-23 02:44:28

标签: reactjs components state

我正在制作向导表格,但我无法获得第一个名为tableResults的组件来加载。我有一个名为step的状态,它根据组件而变化。目前,步骤的初始状态设置为1.当状态为1时,如何使tableResults显示?

Step1.js片段

import tableResults from './tableResults';

class Step1 extends Component {
    constructor(props) {
      super(props);
     this.state = {
      step: 1
    }
  }


render() {
  const {
    handleSubmit,
    previousPage,
    step
  } = this.props;


    return (  
      <form onSubmit={handleSubmit}>

        <div className="step-container">

          {step === 1 && <tableResults/>}
          </div>
      </form>
    );
  }
}

表格摘要:

import React, { Component, PropTypes } from 'react'

class tableResults extends Component {
  constructor(props) {
    super(props)
    }

  render() {

    return (  
      <div>
        This is the table
      </div>
      );
  }
}

1 个答案:

答案 0 :(得分:2)

你从道具获得状态,但你应该从州获得它,如下所示:

&#13;
&#13;
import tableResults from './tableResults';

class Step1 extends Component {
    constructor(props) {
      super(props);
     this.state = {
      step: 1
    }
  }


render() {
  const {
    handleSubmit,
    previousPage,
    step
  } = this.props;


    return (  
      <form onSubmit={handleSubmit}>

        <div className="step-container">

          {this.state.step === 1 && <tableResults/>}
          </div>
      </form>
    );
  }
}
&#13;
&#13;
&#13;