reactjs无法将this.pops传递到react图表列表中
reactjs unable to pass down this.props into react-chartist
我想把this.state从"main.js"(父组件)传递到"bar.js"中(子组件)。
//main.js
import React, { Component } from 'react';
import BarChart from './Bar-chart';
class Hero extends Component {
constructor(props) {
super(props);
this.state = {
labels: ['P1', 'P2', 'P3', 'P4', 'P5/P6'],
series: [[ 1, 2, 3, 4, 5 ]]
}
}
render() {
return (
<div className="header">
<div className="container">
<div className="row">
<BarChart data={this.props.labels, this.props.series}/>
</div>
</div>
</div>
</div>
);
}
};
export default Hero;
这是我的子组件:
//bar.js
import React, { Component } from 'react';
import ChartistGraph from 'react-chartist';
import Legend from 'chartist-plugin-legend';
class BarGraph extends Component {
constructor(props) {
super(props);
}
render() {
const option = {
height: '350px',
plugins: [
Legend({
legendNames: ['P1', 'P2', 'P3', 'P4', 'P5/P6'],
})
]
};
return (
<ChartistGraph
data={this.props.labels, this.props.series}
options={option}
type={'Bar'} />
);
}
barData() {
return ({
labels: ['P1', 'P2', 'P3', 'P4', 'P5/P6'],
series: [[ 8, 28, 40, 25, 9 ]]
});
};
}
export default BarGraph;
此外,我仍然有点困惑于什么时候应该使用this.state和this.pops。在这种情况下,我使用this.pop是否正确?
根据您传递道具的方式,道具的结构并不像您期望的那样。
试着把道具的结构改成这样:
<BarChart data={{ labels: this.props.labels, series: this.props.series}}/>
从本质上讲,这是将一个带有标签和序列键的对象传递给您的子组件。外部大括号意味着它们内部的所有内容都将被评估为JavaScript。所以我们放了更多的大括号来表示我们正在传递一个对象。
现在,在您的嵌套组件上,您应该可以访问以下结构。操作:
this.props = {
series: [],
labels: []
}
然而,由于您的父状态的结构与此图表图表所需的完全相同(具有标签数组和序列数组),如果您想直接传递图表图表的数据对象,请执行以下操作:
<BarChart data={this.state} />
你可以这样渲染你的图形:
<ChartistGraph
data={this.props}
options={option}
type={'Bar'} />
相关文章:
- reactjs无法将this.pops传递到react图表列表中
- 为什么在React中,todo列表的显示如此脆弱
- React中的筛选列表
- 在React中呈现列表
- 使用 Selenium 和 java 测试 React JS 下拉列表
- OnChange 事件使用 React JS 作为下拉列表
- react使用扩散属性渲染组件列表
- React Redux将项目添加到列表中会转移到其他创建的列表中
- 如何获得Materialize select下拉列表以使用React
- 如何使用react bootstrap创建动态下拉列表
- 在 React js 中打开和关闭递归嵌套列表
- 将 Picker 绑定到 React Native 中的 Picker.Item 列表
- 从列表视图传递 React 本机状态
- 如何使用 React JS 迭代由 Java 休眠 DAO 返回的列表,该 DAO 在 UI 中以 this.state
- react native:可选择的部分列表视图出现错误
- React.js配方用于渲染大量增量项目列表
- 如何使用react.js返回多个列表组件
- 使用React突出显示列表中的项目时的性能
- 大列表中React子节点上的重复键对性能的影响
- React列表的渲染顺序必须一致