I am using recharts, and if the data variable is outside of a class, it works fine. What happens is it loads, animates the graph, and shows the "dot" coordinates. However, if the data variable is inside the class, it does not animate, and does not update the "dot" coordinates css.
Notice the commented out data in the render method, if I uncomment that, and comment out the top data variable, it doesn't work, but this current setup works just fine. Any ideas for a fix? I eventually want to load this.props.data instead of data once this is fixed.
const data = [{ name: '07/14/2017', mood: 6 }, { name: '07/15/2018', mood: 7 }];
class LinearGraph extends Component {
constructor(props) {
super(props);
}
render() {
//const data = [{ name: '07/14/2017', mood: 6 }, { name: '07/15/2018', mood: 7 }];
return (
<ResponsiveContainer width="100%" height="80%">
<LineChart
data={data}
margin={{ top: 5, right: 50, left: 0, bottom: 5 }}
>
<XAxis dataKey="name" />
<YAxis />
<CartesianGrid
strokeDasharray="3 3"
horizontal={false}
/>
<Tooltip />
<Legend />
<Line
type="monotone"
dataKey="mood"
stroke="rgba(43, 191, 217, 0.9)"
dot={{ stroke: '#ff0000', strokeWidth: 12 }} // this isn't working at all
activeDot={{ r: 1 }}
strokeWidth={5}
/>
<ReferenceLine y={7} label="Max" stroke="green" strokeDasharray="3 3" />
</LineChart>
</ResponsiveContainer>
);
}
}
also for a more visual understanding, in this photo it's when it works (can't show the animation, but you can see the "dot" coordinates are working):
EDIT: I've also tried setting the state in componentWillMount (and componentDidMount but this latter gave a warning):
class LinearGraph extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
};
}
componentWillMount() {
this.setState({ data: this.props.data });
}
render() {
return (
<ResponsiveContainer width="100%" height="80%">
<LineChart data={this.state.data} />
</ResponsiveContainer>
);
}
}


dataas a property of your component'sstate?initialStatein your constructor. If the library doesn't like an empty array, can you initiate it as an array w/ a single empty object, likedata: [{}]? (Admittedly, w/o knowing the inner-workings of the library, that's a bit of a shot in the dark)