Through my previous question I learned that React preserves the state for child components automatically and that's why its documentation says:
What Shouldn't Go in State?
React components: Build them in render() based on underlying props and state.
Now my question is how to do the same for components that we are just hiding?
Consider a child component that in an iteration is not going to be shown and at the same time we would like to preserve its state for the future when it is going to be brought back!
To illustrate what I exactly mean I've designed a case scenario to show it to you. In the following code you can add stateful child components. A checkbox is provided for each component so you can flag them. Finally the third button will hide the child components that are not flagged. I'm looking for a way to restore the state for the not-flagged components once they are brought back.
class BlackBox extends React.Component
{
constructor() {
super();
this.state = {
checked: false,
counter: 0,
};
}
increment = () => {
this.setState(Object.assign({}, this.state, { counter: this.state.counter+1 }));
};
switch = () => {
this.setState(Object.assign({}, this.state, { checked: !this.state.checked }));
};
isChecked() {
return this.state.checked;
}
render() {
return (
<span onClick={this.increment} title={this.props.id} style={{
fontSize: '24pt',
border: '1px solid black',
margin: 10,
padding: 10,
}}>
<input type="checkbox" onChange={this.switch} checked={this.state.checked} />
{this.state.counter}
</span>
);
}
}
class RedBox extends React.Component
{
constructor() {
super();
this.state = {
checked: false,
counter: 0
};
}
increment = () => {
this.setState(Object.assign({}, this.state, { counter: this.state.counter+1 }));
};
switch = () => {
this.setState(Object.assign({}, this.state, { checked: !this.state.checked }));
};
isChecked() {
return this.state.checked;
}
render() {
return (
<span onClick={this.increment} title={this.props.id} style={{
fontSize: '24pt',
border: '1px solid red',
margin: 10,
padding: 10,
}}>
<input type="checkbox" onChange={this.switch} checked={this.state.checked} />
{this.state.counter}
</span>
);
}
}
class Parent extends React.Component {
static blackCount = 0;
static redCount = 0;
state = {
childCmps: [],
showOnlyChecked: false,
};
constructor(props, context) {
super(props, context);
}
addBlackBox = () => {
this.setState(Object.assign({}, this.state, {
childCmps: [...this.state.childCmps, { Component: BlackBox, id: "black" + (++Parent.blackCount) }],
}));
};
addRedBox = () => {
this.setState(Object.assign({}, this.state, {
childCmps: [...this.state.childCmps, { Component: RedBox, id: "red" + (++Parent.redCount) }],
}));
};
showHide = () => {
this.setState(Object.assign({}, this.state, {
showOnlyChecked: !this.state.showOnlyChecked,
}));
};
render() {
let children = this.state.childCmps.map(child => <child.Component key={child.id} id={child.id} ref={child.id} />);
return (
<div>
<button onClick={this.addBlackBox}>Add Black Box</button>
<button onClick={this.addRedBox}>Add Red Box</button>
<button onClick={this.showHide}>Show / Hide Unchecked</button>
<br /><br />
{children.filter(box => !this.state.showOnlyChecked || this.refs[box.props.id].isChecked())}
</div>
);
}
}
ReactDOM.render(
<Parent />,
document.getElementById("root")
);