So, I have a few components passing down a function to update the main component's state and then I want to re-render on the grandchild's component...
class GrandParentComponent extends Component {
state = {
formData: {
rooms: [],
},
}
// has a function: handleRoom
handleRoom = room => {
let roomsArray = this.state.formData.rooms.slice();
let newRooms = [...roomsArray, room]; // add room to array
this.setState({
formData: update(this.state.formData, { rooms: { $set: newRooms }}),
}); // update and $set comes from 'immutability-helper'
}
</GrandParentComponent>
Passes down to ParentComponent
<ParentComponent handleRoom={this.handleRoom} formData={this.state.formData} />
Passes down to ChildComponent
<ChildComponent handleRoom={this.props.handleRoom} />
In thie child component, I want to update the state of the grandparent component.
handleSaveRoom = room => { this.props.handleRoom(room); }
// great this successfully updates the grandparent...
After the grandparent component is updated, I want to re-render the ParentComponent using the new data:
<ParentComponent handleRoom={this.handleRoom} formData={this.state.formData} >
handleCreateAddRoomForm = (room, i = 0) => {
return (
<Panel header={room.title} key={i}>
<AddRoomForm room={room} />
</Panel>
)
}
render() {
const { formData } = this.props;
const renderRooms = formData.rooms.length
? formData.rooms.map((room, i) => {
this.handleCreateAddRoomForm(room, i);
})
: this.handleCreateAddRoomForm({title: 'New Room'});
return (
<div>
{renderRooms}
</div>
)
}
</ParentComponent>
So, I get back the room found... not sure about the (13 Not Found)... but the <Panel /> does not show in the <ParentComponent />
Ideally, when the data is updated on the <Grandparent /> from the <Child /> the <Parent /> should create a <Panel /> component to show the change...
Thoughts?