我想将道具从我的父组件发送到子组件。这是我在父组件状态中使用的方式-->
chartArray:[
{
id:0,
chart:'LineChart',
device:1,
sensor:1,
},
{
id:1,
chart:'LineChart',
device:2,
sensor:4,
},]
在父组件渲染方法中->我使用图表组件并发送这些道具
{
chartArray.map((obj,index)=>{
return(
<Grid item xs={12} md={12} key={index}
className={classes.mainGrid}
style={{paddingBottom:30}}
>
<div>
<div >
<div>
<Typography
variant={"h5"}
>
Device Id: {obj.device}
<IconButton style={{float:'right'}}
onClick={this.handleDeleteChart.bind(this,index)}
>
<DeleteIcon fontSize="small" />
</IconButton>
</Typography>
</div>
</div>
<div>
<Typography variant={"subtitle1"}>
Sensor: Temperature
</Typography>
</div>
</div>
<Paper elevation={3}
style={{
overflowX: 'scroll',
}}
>
**<Chart
chartType={obj.chart}
sensorId={obj.sensor}
deviceId={obj.device}
/>**
</Paper>
</Grid>
)
}
)}
在子组件中-->
导出默认类图表扩展组件{
constructor(props){
super(props);
this.state={
id:0,
chart:props.chartType,
device:props.deviceId,
sensor:props.sensorId,
}
}
async componentDidMount(){
const response2 = await fetch(`/api/SensorData/${this.state.device}/${this.state.sensor}`)
const bodySensors = await response2.json()
const labels1=[]
const data1=[]
bodySensors.map((chartdata)=>{
return(
labels1.push(chartdata.date),
data1.push(chartdata.value)
)
})
this.setState({
dataLineChart:{
labels:[...labels1],
datasets:[
{
fill:false,
label:'Temperature',
data:[...data1],
backgroundColor:'rgba(210, 77, 87, 1)',
borderColor:'rgba(137, 196, 244, 1)',
pointBorderWidth:1,
pointHoverRadius:10,
pointHoverBackgroundColor:'rgba(210, 77, 87, 1)',
pointHoverBorderColor:'rgba(137, 196, 244, 1)',
pointHoverBorderWidth:2,
pointRadius:2,
// how much closer to pop up point
pointHitRadius:10
// steppedLine:true
}
]
}
})
}
render() {
const{chartType}=this.props
const {dataLineChart} = this.state
if (chartType==='BarChart')
return (
<BarChart />
)
else if (chartType==='LineChart')
return (
<LineChart ccData={dataLineChart}/>
)
else if (chartType==='PieChart')
return (
<PieChart/>
)
}
}
当我使用这种方式在渲染功能之外使用这些道具时。有用。当我将对象添加到父组件状态时-> chartArray 它也可以工作。但是,当我从该 chartArray 中删除某些内容时,它不会向子组件发送道具吗?我在这里真的很困惑。请帮忙。谢谢