Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does React diff algorithm fails when using inline style

While using react inline-style most people what they do is use object in styles attribute. for eg. <div style={{left: '54px', position: 'absolute'}}> </div>

Does this react diff algorithm fails over here as their is new object created everytime it re-renders.

like image 892
Priyank Bhatt Avatar asked Nov 11 '16 06:11

Priyank Bhatt


People also ask

Is inline styling in React bad?

One of the main reasons that inline styling is not a good choice for your application is because it does not support (or it has really poor support) for CSS features. Every application nowadays might have to end up using some selectors such as :hover , :active , :focused , etc.

Which of the following Cannot be used with React inline styles?

Inline styles cannot be used to target pseudo-classes or pseudo-elements.

Can you inline style with React component?

React lets you use "inline styles" to style your components; inline styles in React are just JavaScript objects that you can render in an element's style attribute. The properties of these style objects are just like the CSS property, but they are camel case ( borderRadius ) instead of kebab-case ( border-radius ).

How does diff algorithm work in React?

React uses a heuristic algorithm called the Diffing algorithm for reconciliation based on these assumptions: Elements of different types will produce different trees. We can set which elements are static and do not need to be checked.


1 Answers

YES, It will affect the diff algorithm

Like you said you are creating an Object every time when you re-render.

But, when you do the following

const style = {left: '54px', position: 'absolute'}

<div style={style}></div>

you're passing a reference of style which remains the same throughout the component's lifecycle.

This is same for arrow functions. Read more about this here

like image 83
Pranesh Ravi Avatar answered Oct 02 '22 01:10

Pranesh Ravi