|
303 | 303 | |287| [What is diffing algorithm?](#what-is-diffing-algorithm)|
|
304 | 304 | |288| [What are the rules covered by diffing algorithm?](#what-are-the-rules-covered-by-diffing-algorithm)|
|
305 | 305 | |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)| |
306 | 309 |
|
307 | 310 | ## Core React
|
308 | 311 |
|
|
4873 | 4876 | 1. Managing focus, text selection, or media playback.
|
4874 | 4877 | 2. Triggering imperative animations.
|
4875 | 4878 | 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. |
4877 | 4918 |
|
0 commit comments