I want to send props from my parent component to child component. this is the way i have used In parent component state-->
chartArray:[
{
id:0,
chart:'LineChart',
device:1,
sensor:1,
},
{
id:1,
chart:'LineChart',
device:2,
sensor:4,
},]
In parent component render method --> I user Chart component and send those props
{
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>
)
}
)}
In child component-->
export default class Chart extends Component {
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/>
)
}
}
when i use this way to use those props outside render function. It works. When I add objects to parent component state--> chartArray it also works. But when ever i delete something from that chartArray it does not send props to child component? I'm really confused here. please help. Thank you