Skip to content

Commit

Permalink
Updating question - How would you prevent a component from rendering?
Browse files Browse the repository at this point in the history
  • Loading branch information
Lula Leus committed Nov 22, 2018
1 parent 5158e81 commit 4ed6053
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,17 @@ You can use property initializers to correctly bind callbacks. This is enabled b
you can use an arrow function in the callback. The problem here is that a new callback is created each time the component renders.

#### How would you prevent a component from rendering?
Returning null from a component's render method does not affect the firing of the component's lifecycle methods.
Returning null from a component's render method hide the component, but it does not affect the firing of the component's lifecycle methods.

If the amount of the component re-rendering is an issue, there are two options available. Manually implementing a check in shouldComponentUpdate lifecycle method hook.
```
shouldComponentUpdate(nextProps, nextState){
// Do some check here
return resultOFCheckAsBoolean
}
```
Or using React.PureComponent instead of React.Component
React.PureComponent implements shouldComponentUpdate() with a shallow prop and state comparison. So you can avoid re-rendering the component with the same props and state.
#### When rendering a list what is a key and what is it's purpose?
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity. The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys. When you don't have stable IDs for rendered items, you may use the item index as a key as a last resort. It is not recommend to use indexes for keys if the items can reorder, as that would be slow.

Expand Down

0 comments on commit 4ed6053

Please sign in to comment.