Children is lost when component reRender.
See original GitHub issuecode:
class Table extends React.Component {
render() {
const {dataSource, checked, children} = this.props;
return <div>{
dataSource.map(item => {
return <div>{checked ? 'checked' : ''}{item.name}<em>{children.props.dataIndex}</em></div>
})
}</div>
}
}
function mountMock(element) {
class Wrapper extends React.Component {
state = {
props: element.props,
Component: element.type
};
setProps(props) {
props = {...this.state.props, ...props};
this.setState({
props
});
}
render() {
const {Component} = this.state;
return <Component {...this.state.props} />;
}
}
const div = document.createElement('div');
document.body.appendChild(div);
return ReactDOM.render(<Wrapper/>, div);
}
const dataSource = [{ id: '1', name: 'test' }, { id: '2', name: 'test2' }];
const table = (
<Table dataSource={dataSource}>
<div dataIndex='id'/>
</Table>
);
const inc = mountMock(table);
inc.setProps({
checked: true
});
expected output:
But preact will throw error.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
javascript - React child component state is lost after parent ...
Now I have state in my parent component (companyIcon), which I need to update based on some validation in the child component. I...
Read more >The mystery of React Element, children, parents and re-renders
Now, we know that React components re-render themselves and all their children when the state is updated. In this case, on every mouse...
Read more >Avoiding React component re-renders with React.memo
A component can re-render even if its props don't change. More often than not this is due to a parent component re-rendering causing...
Read more >Does changing the state of a parent component trigger a ...
No. That's the principle of one-way binding. Properties only flow from parent to child. Children cannot mutate their properties. Children however can be ......
Read more >Does a child component re-render because it receives a new ...
you are correct. The only way for a child to get new props is to wait until parent rerenders. And when parent rerenders,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have got as well some problem with children too but with dangerousetinnerHtml ,
Try to apply my patch in preact.js code (yeah i write it in preact-compat issues but it’s more relatif to preact.js Maybe it’s could work in your case too. (because for me it was happen same after rerender of the component)
after apply the patch code , just add innerHTML as a props in <Table innerHTML … >
the code to change is explained in this thread: https://github.com/developit/preact-compat/issues/371
if developit could see it and give a feedback . My website haven’t got issue anymore with dangerousetinnerHtml after that.
Regards
I remove this line code and it work well.
https://github.com/developit/preact/blob/master/src/h.js#L21.
And I want to know why ignore children of props.
https://github.com/developit/preact/commit/61375a07201a614edb2549cdcee7d59880aa9d95