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.

Diffing algo. forces re-render with respect to no dependency on state

See original GitHub issue

Hello,

I created a state variable representing the latest time which is updated every second, and pass it only to components which rely on time.

const main = logger()(app)(state, actions, view, document.body);
setInterval(main.time, 1000);
export default {
    time: () => state => ({time: new Date()}),
    ...order
}

In my main view, I currently reference state.time in only one component. For some reason though, the entire view gets patched from state.time being updated.

export default (state, actions) =>
    <Layout>
        {/* Header */}
        <GridItem x={1} y={1} xs={-1} ys={1}><NavBar time={state.time}/></GridItem>

        {/* Left Column */}
        <GridItem x={1} xs={5} y={2} ys={8}>
            <Panel>
                <Header>Order</Header>
                <OrderForm order={state.order} actions={actions.order}/>
            </Panel>
        </GridItem>
        ...

I found out the whole view was being re-rendered as the functional component <GridItem/> keeps on continuously being called. This also forces picostyle which is what I am using to style the components to regenerate classes for all components being re-rendered every second.

export const GridItem = (props, children) => {
    console.log(props)
    return styled("div")({
        gridColumn: `${props.x || ''}${props.x && props.xs && ' / ' || ''}${props.xs && ((props.xs !== -1 && `span ` || '') + props.xs) || ''}`,
        gridRow: `${props.y || ''}${props.y && props.ys && ' / ' || ''}${props.ys && ((props.ys !== -1 && `span ` || '') + props.ys) || ''}`,
    })
    (props, children);
}

image

diff

Does anyone have any ideas on how to not have state.time trigger an entire re-render?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jorgebucarancommented, Jan 10, 2018

@iwasaki-kenta Does anyone have any ideas on how to not have state.time trigger an entire re-render?

Render means to call the view function to produce a new virtual node tree and then patch the DOM with it. We are —not— creating new elements, only updating their attributes (setAttribute(element, attribute, value)), hence the flashing. To avoid calling the view function when your view is mostly static, I’d suggest to follow @andyrj’s advice and memoize it for now.

I am, however, interested in exploring “auto-memoization” of views, but perhaps in a separate issue? 🙏

1reaction
andyrjcommented, Jan 10, 2018

Also since picostyle is going to do this for every user, adding memoization to that libraries styled() might be neat to consider also.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reconciliation - React
The Diffing Algorithm. When diffing two trees, React first compares the two root elements. The behavior is different depending on the types of...
Read more >
Difference between DOM update and re-render - Stack Overflow
We know, React uses reconciliation and diffing algorithm to update the DOM. Again, if a state changes, react re-render the component and child ......
Read more >
The mystery of React Element, children, parents and re-renders
Looking into what is React Element, exploring various children vs parents relationship in React, and how they affect re-renders.
Read more >
What is Diffing Algorithm ? - GeeksforGeeks
React works on observable patterns, hence, whenever there is a change in the state, it updates the nodes in the virtual DOM; Then,...
Read more >
21 Performance Optimization Techniques for React Apps
React builds and maintains an internal representation of the rendered UI (Virtual DOM). When a component's props or state changes, React ...
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