Skip to content

Commit 4289a74

Browse files
committed
Add render props interview questions
1 parent 7304fde commit 4289a74

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@
303303
|287| [What is diffing algorithm?](#what-is-diffing-algorithm)|
304304
|288| [What are the rules covered by diffing algorithm?](#what-are-the-rules-covered-by-diffing-algorithm)|
305305
|289| [When do you need to use refs?](#when-do-you-need-to-use-refs)|
306+
|290| [Is it prop must be named as render for render props?](#is-it-prop-must-be-named-as-render-for-render-props)|
307+
|291| [What are the problems of using render props with pure components?](#what-are-the-problems-of-using-render-props-with-pure-components)|
308+
|292| [How do you create HOC using render props?](#how-do-you-create-hoc-using-render-props)|
306309

307310
## Core React
308311

@@ -4873,5 +4876,43 @@
48734876
1. Managing focus, text selection, or media playback.
48744877
2. Triggering imperative animations.
48754878
3. Integrating with third-party DOM libraries.
4876-
4879+
290. ### Is it prop must be named as render for render props?
4880+
Even though the pattern named render props, you don’t have to use a prop named render to use this pattern. i.e, Any prop that is a function that a component uses to know what to render is technically a “render prop”. Lets take an example with the children prop for render props,
4881+
```javascript
4882+
<Mouse children={mouse => (
4883+
<p>The mouse position is {mouse.x}, {mouse.y}</p>
4884+
)}/>
4885+
```
4886+
Actually children prop doesn’t need to be named in the list of “attributes” in JSX element. Instead, you can keep it directly inside element,
4887+
```javascript
4888+
<Mouse>
4889+
{mouse => (
4890+
<p>The mouse position is {mouse.x}, {mouse.y}</p>
4891+
)}
4892+
</Mouse>
4893+
```
4894+
While using this above technique(without any name), explicitly state that children should be a function in your propTypes.
4895+
```javascript
4896+
Mouse.propTypes = {
4897+
children: PropTypes.func.isRequired
4898+
};
4899+
```
4900+
291. ### What are the problems of using render props with pure components?
4901+
If you create a function inside a render method, it negates the purpose of pure component. Because the shallow prop comparison will always return false for new props, and each render in this case will generate a new value for the render prop. You can solve this issue by defining the render function as instance method.
4902+
292. ### How do you create HOC using render props?
4903+
You can implement most higher-order components (HOC) using a regular component with a render prop. For example, if you would prefer to have a withMouse HOC instead of a <Mouse> component, you could easily create one using a regular <Mouse> with a render prop.
4904+
```javascript
4905+
function withMouse(Component) {
4906+
return class extends React.Component {
4907+
render() {
4908+
return (
4909+
<Mouse render={mouse => (
4910+
<Component {...this.props} mouse={mouse} />
4911+
)}/>
4912+
);
4913+
}
4914+
}
4915+
}
4916+
```
4917+
This way render props gives the flexibility of using either pattern.
48774918

0 commit comments

Comments
 (0)