1. What is the difference between Component and PureComponent? Give an example where it might break my app.
It's the same thing, PureComponent
it's used for performance optimizations, it's implements lifecycle shouldComponentUpdate
for you and shallow comparison to check if component should re-render.
You can use regular Component
with shouldComponentUpdate
lifecycle manually, but you need to be careful because if you miss some props or state, component can block required re-renders.
So it's better to avoid this type of performance improvements at all, React it's fast enough to do all this rendering stuff, implement all this performance tricks only after measure performance leaks.
class Parent extends React.Component {
render() {
return (
<Child
caption="ImageGallery"
texts={['text1', 'text2']}
/>
);
}
}
class Child extends React.PureComponent {
render() {
return (
<div>
<span>{this.props.caption}</span><br />
{this.props.texts.map((text) => (
<span>{text}</span>
))}
</div>
);
}
}
This is an example where instead of optimizing our Child
component we will do less efficient code.
PureComponent
will do only shallow comparison, in our case texts
prop is an array and for each re-render it's will create a new array even if contains the same value.
I think SCU can block context propagation. So basically we can shouldComponentUpdate
to fix some performance issues and block rendering except some specific props needed for this component. When we will have some changes in our context, then our component will not be able to re-render.
- Create a callback function that change something in parent state and pass this function to child to be called.
- React Context
- Other state management solutions like Redux, Mobx, Jotai, Zustand and others.
shouldComponentUpdate
orPureComponent
for class componentsReact.memo()
for functional componentsuseCallback()
oruseMemo()
hooks for handling not shallow comparison.
Fragments can group multiple children elements without adding any node to DOM. A good example can be a table
<table>
<tr>
<SomeComponent />
</tr>
</table>
function SomeComponent() {
return (
<>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</>
)
}
So if we remove <></>
fragments in above example we will receive an error, because multiple node elements should be wrapped in a parent node.
The most used cases for HOC pattern is some conditional rendering of JSX or passing some props or state to components. With this pattern we can optimize and remove code duplications and enhance readability of the project.
connect()
function from react-redux library where we pass mapStateToProps or mapDispatchToProps- ANY custom HOC or library like auth0 or amplify, for example to manage permissions or auth state
- ANY custom HOC for Google Analytics to push some events to dataLayer
Example of HOC creation:
const withHigherOrderComponent = (Component) => (props) => <Component {...props} />;
async..await
we can handle errors withtry {} catch {}
construction.promises
we can handle in 2 ways:promise().then().catch()
or if we create the promise from scratch we can usenew Promise(resolve, reject) { reject(new Error()) }
callbacks
we can pass some functions as callbacks to handle success or error states
setState()
can accept 2 params:
- updater we can pass
setState({ count: 1 })
or even a function to access previous state or propssetState((prevState, props) => {})
- a function callback where we can have access to updated state
setState()
it's async because of performance concerns, setting state in sync mode can block rendering and this is something we want to avoid to offer users a better experience.
- use function instead of class
- remove the constructor
- remove the render() method, keep the return
- add const before all methods
- remove this.state throughout the component
- remove all references to ‘this’ throughout the component
- Set initial state with useState()
- change this.setState(), call the function that you named in the previous step to update the state…
- replace componentDidMount with useEffect
- replace componentDidUpdate with useEffect
- replace componentWillUnmount with useEffect
- inline with style attribute
style={{ color: 'red' }}
- using classNames or utility css framework like Tailwind
- using CSS Modules to encapsulate component specific styles
- CSS in JS using libraries like styled components or emotion
with dangerouslySetInnerHTML
prop, we need also to pass an object with __html
key
<Text dangerouslySetInnerHTML={{ __html: '<h1>Hello world!</h1>' }} />