Dva: 请教组件用推荐规范后获取默认状态的方法如何写?

Created on 20 Dec 2016  ·  1Comment  ·  Source: dvajs/dva

下面是之前的写法

const ChartDashBoard = React.createClass({
  // 初始化
  getInitialState() {
    return {
      height: 200,
      plotCfg: {
        margin: [60, 70, 40, 10],
      },
    };
  },
  // 渲染
  render() {
    return (
      <div>
        <DashBoard
          forceFit={true}//宽度自动
          height={this.state.height}//高度
          plotCfg={this.state.plotCfg}//边框背景等配置
          ref="myChart"
        />
      </div>
    );
  },
});

请问用
const Chart = (props) => {}
这种方式命名的组件如何获取默认状态?

难道是下面这种写法?

const Chart = (props) => {

 // 初始化
  getInitialState() {
    return {
      height: 200,
      plotCfg: {
        margin: [60, 70, 40, 10],
      },
    };
  },

return (
    <ECharts height ={height} />
  );
}

Most helpful comment

纯函数组件是和状态无关的,所需数据都来自父级组件

class App extends Component {
   constructor(){
     this.state = {
      height: 200,
      plotCfg: {
        margin: [60, 70, 40, 10],
      },
     }
  }

  render() {
       return  <Chart  {...this.state}/>
   }

}

const Chart = (props) => {
  const {height} = props
  return (
    <ECharts height ={height} />
  );
}

>All comments

纯函数组件是和状态无关的,所需数据都来自父级组件

class App extends Component {
   constructor(){
     this.state = {
      height: 200,
      plotCfg: {
        margin: [60, 70, 40, 10],
      },
     }
  }

  render() {
       return  <Chart  {...this.state}/>
   }

}

const Chart = (props) => {
  const {height} = props
  return (
    <ECharts height ={height} />
  );
}

Was this page helpful?
0 / 5 - 0 ratings