I have a react app with components in the following structure:
<App>
<Toolbar this.state={sectionInFocus: someSectionID }/>
<Chart>
<Section> <input value=someSectionID /> <Section />
<Section> <input value=someSectionID /> <Section />
<Section> <input value=someSectionID /> <Section />
...more sections...
</Chart>
</App>
I wish to adopt a functionality where the state of the toolbar is set when the input buttons in the sections are focused on (onFocus).
How do I set the state of toolbar to the id of the last selected section? Do I set the state in each section, passing back 2 levels up then to toolbar?
I have tried to at least set state in the root component to the focused sectionID, but now I am getting another error message (maximum call stack reached), like so:
App.js:
import React, { Component } from 'react';
import Chart from './chart';
export default class App extends Component {
constructor(props){
super(props)
this.state = {
sectionInFocus: ''
}
}
componentDidUpdate(){
console.log(this.state); // maximum call stack size exceeded
}
chartUpdate(obj){
const { sectionInFocus } = obj;
console.log(sectionInFocus);
this.setState({ sectionInFocus });
}
render() {
return (
<div>
<Chart chart={{"chartData":["abc","def","ghi"]}} onUpdate={this.chartUpdate.bind(this)}/>
</div>
)
}
};
Chart.js
import React, { Component } from 'react';
import Section from './section';
export default class Chart extends Component {
constructor(props) {
super(props);
this.state = {
sectionInFocus: ""
}
}
componentDidUpdate(){
console.log(this.state.sectionInFocus);
this.props.onUpdate(this.state);
}
sectionUpdate(sectionID) {
this.setState({sectionInFocus: sectionID});
}
renderSections(chartData) {
return chartData.map((section) => {
return (
<Section
key = {section}
sectionID = {section}
onUpdate={this.sectionUpdate.bind(this)}
/>
)
})
}
render(){
let chartData = this.props.chart.chartData;
return (
<div>
{this.renderSections(chartData)}
</div>
)
}
}
Section.js
import React, { Component } from 'react';
export default class Section extends Component {
updateParentState(){
this.props.onUpdate(this.props.sectionID)
}
render(){
return (
<div>
<input
value={this.props.sectionID}
onFocus={this.updateParentState.bind(this)}
/>
</div>
)
}
}
What am I doing wrong that I am getting the maximum call stack error message? Is there an easier way to go about handling this sort of functionality, or is this kind of the react way of doing things? Should I be looking into redux just to manage this efficiently?
Any help provided is greatly appreciated!