Diffing algo. forces re-render with respect to no dependency on state
See original GitHub issueHello,
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);
}
Does anyone have any ideas on how to not have state.time
trigger an entire re-render?
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top 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 >
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 Free
Top 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
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 theview
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? 🙏
Also since picostyle is going to do this for every user, adding memoization to that libraries styled() might be neat to consider also.