question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Children is lost when component reRender.

See original GitHub issue

code:

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:

image

But preact will throw error.

image

image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
simonjoomcommented, Jul 4, 2017

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

0reactions
windyGexcommented, Jul 18, 2017
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found