[
  {
    "path": ".all-contributorsrc",
    "content": "{\n  \"projectName\": \"react-advanced-patterns\",\n  \"projectOwner\": \"manjula91\",\n  \"repoType\": \"github\",\n  \"repoHost\": \"https://github.com\",\n  \"files\": [\n    \"README.md\"\n  ],\n  \"imageSize\": 100,\n  \"commit\": true,\n  \"contributors\": [\n    {\n      \"login\": \"manjula91\",\n      \"name\": \"Manjula Subhashchandra Dube\",\n      \"avatar_url\": \"https://avatars0.githubusercontent.com/u/7845921?v=4\",\n      \"profile\": \"https://www.linkedin.com/profile/view?id=181968473&trk=nav_responsive_tab_profile\",\n      \"contributions\": [\n        \"doc\",\n        \"code\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"vivek12345\",\n      \"name\": \"Vivek Nayyar\",\n      \"avatar_url\": \"https://avatars3.githubusercontent.com/u/4931048?v=4\",\n      \"profile\": \"https://www.viveknayyar.in/\",\n      \"contributions\": [\n        \"doc\",\n        \"code\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"ryands17\",\n      \"name\": \"Ryan Dsouza\",\n      \"avatar_url\": \"https://avatars2.githubusercontent.com/u/19697099?v=4\",\n      \"profile\": \"https://github.com/ryands17\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"meghna-saxena\",\n      \"name\": \"Meghna Srivastava\",\n      \"avatar_url\": \"https://avatars3.githubusercontent.com/u/18181755?v=4\",\n      \"profile\": \"https://meghnasrivastava.herokuapp.com\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"EdwardDrapkin\",\n      \"name\": \"Edward Drapkin\",\n      \"avatar_url\": \"https://avatars1.githubusercontent.com/u/6296006?v=4\",\n      \"profile\": \"https://github.com/EdwardDrapkin\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"felipediogo\",\n      \"name\": \"Felipe F. Diogo\",\n      \"avatar_url\": \"https://avatars3.githubusercontent.com/u/26486135?v=4\",\n      \"profile\": \"https://github.com/felipediogo\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"alexrohleder\",\n      \"name\": \"Alex Rohleder\",\n      \"avatar_url\": \"https://avatars2.githubusercontent.com/u/7248028?v=4\",\n      \"profile\": \"https://www.alexrohleder.com\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"5achinJani\",\n      \"name\": \"Sachin Jani\",\n      \"avatar_url\": \"https://avatars3.githubusercontent.com/u/23077544?v=4\",\n      \"profile\": \"https://twitter.com/5achinJani\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"garetmckinley\",\n      \"name\": \"Garet McKinley\",\n      \"avatar_url\": \"https://avatars0.githubusercontent.com/u/1624279?v=4\",\n      \"profile\": \"https://garet.io\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    },\n    {\n      \"login\": \"kuldeepkeshwar\",\n      \"name\": \"anotherjsguy\",\n      \"avatar_url\": \"https://avatars1.githubusercontent.com/u/10448534?v=4\",\n      \"profile\": \"https://in.linkedin.com/in/kuldeepkeshwar\",\n      \"contributions\": [\n        \"doc\",\n        \"example\"\n      ]\n    }\n  ]\n}\n"
  },
  {
    "path": ".gitignore",
    "content": "node_modules"
  },
  {
    "path": "README.md",
    "content": "# <h1 align=\"center\">React Advanced Patterns </h1>\n\n[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)\n\n<p> This repo is maintained to have collection of react patterns & best practices used in the community. This repo provides you the collated list of all the patterns used in the community. If You have discovered one & would love to share your own react pattern simply send us a PR 🙌 </p>\n\nThis is highly inspired from [Michael Jackson](https://github.com/mjackson) & [Kent C. Dodds.](https://twitter.com/kentcdodds) So special thanks to them for the lovely react patterns in life 😊\n\n#### React Advanced Patterns\n\n- [Compound Components](https://github.com/manjula91/react-advanced-patterns/tree/master/compound-components)\n- [Flexible Compound Components](https://github.com/manjula91/react-advanced-patterns/tree/master/flexible-compound-components)\n- [Prop Collection](https://github.com/manjula91/react-advanced-patterns/tree/master/prop-collection)\n- [Prop Getters](https://github.com/manjula91/react-advanced-patterns/tree/master/prop-getters)\n- [Render Props](https://github.com/manjula91/react-advanced-patterns/tree/master/render-props)\n- [State Reducers](https://github.com/manjula91/react-advanced-patterns/tree/master/state-reducers)\n- [Higher Order Components](https://github.com/manjula91/react-advanced-patterns/tree/master/hoc)\n- [Provider Pattern](https://github.com/manjula91/react-advanced-patterns/tree/master/provider-pattern)\n- [React Hooks](https://github.com/manjula91/react-advanced-patterns/tree/master/hooks)\n\n#### Best Practices when you React\n\n- [Functional setState](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#function-in-setstate)\n- [Get Rid of Unnecessary updates in React](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#why-did-you-update)\n- [Destructuring and spreading props](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#prop-spread)\n- [Runtime type checking with prop types](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#prop-types)\n- [Using defaultProps](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#default-props)\n- [Using displayName](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#display-name)\n- [Conditional Rendering](https://github.com/manjula91/react-advanced-patterns/tree/master/react-best-practices/#conditional-rendering)\n\nIf you want to propose a pattern you should submit it with below points:\n\n- What is so special about this pattern?\n- Use case or inspired from ?\n- Example ([CodesandBox](https://codesandbox.io/) preferred)\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):\n\n<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->\n<!-- prettier-ignore -->\n| [<img src=\"https://avatars0.githubusercontent.com/u/7845921?v=4\" width=\"100px;\"/><br /><sub><b>Manjula Subhashchandra Dube</b></sub>](https://www.linkedin.com/profile/view?id=181968473&trk=nav_responsive_tab_profile)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=manjula91 \"Documentation\") [💻](https://github.com/manjula91/react-advanced-patterns/commits?author=manjula91 \"Code\") [💡](#example-manjula91 \"Examples\") | [<img src=\"https://avatars3.githubusercontent.com/u/4931048?v=4\" width=\"100px;\"/><br /><sub><b>Vivek Nayyar</b></sub>](https://www.viveknayyar.in/)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=vivek12345 \"Documentation\") [💻](https://github.com/manjula91/react-advanced-patterns/commits?author=vivek12345 \"Code\") [💡](#example-vivek12345 \"Examples\") | [<img src=\"https://avatars2.githubusercontent.com/u/19697099?v=4\" width=\"100px;\"/><br /><sub><b>Ryan Dsouza</b></sub>](https://github.com/ryands17)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=ryands17 \"Documentation\") [💡](#example-ryands17 \"Examples\") | [<img src=\"https://avatars3.githubusercontent.com/u/18181755?v=4\" width=\"100px;\"/><br /><sub><b>Meghna Srivastava</b></sub>](https://meghnasrivastava.herokuapp.com)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=meghna-saxena \"Documentation\") [💡](#example-meghna-saxena \"Examples\") | [<img src=\"https://avatars1.githubusercontent.com/u/6296006?v=4\" width=\"100px;\"/><br /><sub><b>Edward Drapkin</b></sub>](https://github.com/EdwardDrapkin)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=EdwardDrapkin \"Documentation\") [💡](#example-EdwardDrapkin \"Examples\") | [<img src=\"https://avatars3.githubusercontent.com/u/26486135?v=4\" width=\"100px;\"/><br /><sub><b>Felipe F. Diogo</b></sub>](https://github.com/felipediogo)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=felipediogo \"Documentation\") [💡](#example-felipediogo \"Examples\") | [<img src=\"https://avatars2.githubusercontent.com/u/7248028?v=4\" width=\"100px;\"/><br /><sub><b>Alex Rohleder</b></sub>](https://www.alexrohleder.com)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=alexrohleder \"Documentation\") [💡](#example-alexrohleder \"Examples\") |\n| :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n| [<img src=\"https://avatars3.githubusercontent.com/u/23077544?v=4\" width=\"100px;\"/><br /><sub><b>Sachin Jani</b></sub>](https://twitter.com/5achinJani)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=5achinJani \"Documentation\") [💡](#example-5achinJani \"Examples\") | [<img src=\"https://avatars0.githubusercontent.com/u/1624279?v=4\" width=\"100px;\"/><br /><sub><b>Garet McKinley</b></sub>](https://garet.io)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=garetmckinley \"Documentation\") [💡](#example-garetmckinley \"Examples\") | [<img src=\"https://avatars1.githubusercontent.com/u/10448534?v=4\" width=\"100px;\"/><br /><sub><b>anotherjsguy</b></sub>](https://in.linkedin.com/in/kuldeepkeshwar)<br />[📖](https://github.com/manjula91/react-advanced-patterns/commits?author=kuldeepkeshwar \"Documentation\") [💡](#example-kuldeepkeshwar \"Examples\") |\n\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n\nThis project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!\n"
  },
  {
    "path": "compound-components/README.md",
    "content": "# Compound Components\r\n\r\n### What are Compound Components?\r\n\r\nCompound Component is a pattern in React where a component doesn't work by itself and needs other specific components to be its children or parents in order to operate. But they still have the control to do lot of stuff on them :)\r\nIt is a pattern in which components are used together such that they share an implicit state that let’s them communicate with each other in the background.\r\n\r\n> Think of compound components like the ```<select>``` and ```<option>``` elements in HTML. Apart they don't do too much, but together they allow you to create the complete experience — [Kent C. Dodds.](https://twitter.com/kentcdodds)\r\n\r\n### What is so special about this pattern\r\n\r\nCompound Component allows us as a developer to have a full control over the rendering behavior, that gives us, ability to decide in what order the component should render. \r\n\r\nCompound Component also reduces the tension of passing tons & tons of configuration as a prop.\r\nTake a look at the documentation for semantic-ui-react, their Form component is all about Compound Component.\r\n\r\n```jsx\r\nimport { Form } from 'semantic-ui-react'\r\n\r\nconst Usage = () => (\r\n  <Form>\r\n    <Form.Group>\r\n      <Form.Input />\r\n      <Form.Select />\r\n      <Form.Button />\r\n    </Form.Group>\r\n  </Form>\r\n)\r\n\r\n```\r\n\r\n\r\n### Code Sandbox Example\r\n\r\nBelow is an example of grouping the RadioButton highly inspired by [semantic-ui-react](https://react.semantic-ui.com/collections/form/)\r\n\r\n[Grouping the radio buttons](https://codesandbox.io/s/z32mw29474)\r\n"
  },
  {
    "path": "flexible-compound-components/README.md",
    "content": "# Flexible Compound Components\r\n\r\nWhat are Flexible Compound Components?\r\n\r\n* This is better version of compound components which makes use of context API and compound component together. This will work even if your order of component changes (which is quite often). This will basically allow user to have more flexibility by using React context to share the implicit state to our child components. We will start with defining childContextTypes, providing the value with getChildContext, and, on each of the components that need that context, we define contextTypes so that React will pass the context that is being asked for. This is more flexible than just using compound components. The consumer component are basically designed in a way that they use render prop in order to work.\r\n\r\n### Example\r\n\r\nThis is an example of grouping the RadioButton highly inspired by [semantic-ui-react](https://react.semantic-ui.com/collections/form/). \r\n\r\n[Grouping the radio buttons](https://codesandbox.io/s/z304lk196l)\r\n\r\n"
  },
  {
    "path": "hoc/README.md",
    "content": "# Higher Order Components\n\n### What are Higher Order Components\n\nHigher order components are a pattern in which a function accepts a component and returns an enhanced version of the component by injecting the required props.\n\n### What is so special about this pattern\n\nHOC's are a pattern used a lot. The most common being the `connect` method of `react-redux` or `withRouter` of `react-router-dom`. This pattern wraps the given Component and returns the same component just by adding extra props to it. This type of function should be pure as it cannot modify the original component, just enhance it.\n\nThe drawback to HOC's is prop-collision. A component wrapped with two or more higher order methods could have props of the same name and any one of those could be overriden. This is where render-props comes to the rescue.\n\nFor prop-collision, lets take the same example as the one in the example section. What if instead of one set of data, I have to render two sets of data, which means I need two fetch hoc's.\n\nThe code will be something like this -\n\n```jsx\nlet App = fetchHOC({ url: 'my-url-1' })(App)\nApp = fetchHOC({ url: 'my-url-2' })(App)\n```\n\nThe data prop being the same will cause just one set of data to be displayed, which is incorrect. Now take a look at the render-prop example and see for yourself that this case can be easily handled.\n\n### Example\n\nThis example demonstrates how we can use the Fetch API as a higher order component.\n\n[Fetch using HOC](https://codesandbox.io/s/734060mlm6)\n"
  },
  {
    "path": "hooks/README.md",
    "content": "# Hooks\n\n### What are hooks ?\n\nHooks are a new feature proposal that lets you use state and other React features without writing a class. They’re currently in React v16.7.0-alpha and being discussed in an open RFC.\n\n### What is so special about hooks?\n\nWith Hooks, you can extract stateful logic from a component so it can be tested independently and reused. Hooks allow you to reuse stateful logic without changing your component hierarchy. This makes it easy to share Hooks among many components or with the community.\n\nAn example of this can be the useState hook. With this hook you no-longer would need a class component to maintain state.\nA functional component with useState hook can maintain state and also perform some side-effects like data-fetching using another well known hook called as useEffect\n\n```jsx\nimport { useState } from \"react\";\n\nfunction Example() {\n  // Declare a new state variable, which we'll call \"count\"\n  const [count, setCount] = useState(0);\n\n  return (\n    <div>\n      <p>You clicked {count} times</p>\n      <button onClick={() => setCount(count + 1)}>Click me</button>\n    </div>\n  );\n}\n```\n\nTo read more about hooks, refer to this [link](https://reactjs.org/docs/hooks-intro.html\n)\n\nArticle [link](https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889) written by Dan Abramov\n\n\n### Example\n\nHere's the codesanbox\n\n[useState](https://codesandbox.io/s/l7407qjzjm)\n\n[useEffect and useReducer](https://codesandbox.io/s/6x41m44wqz)\n\n### Community created hooks\n\n[react-use](https://github.com/streamich/react-use)\n\n[the-platform](https://github.com/palmerhq/the-platform)\n\n[react-hanger](https://github.com/kitze/react-hanger)\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"react-advanced-patterns\",\n  \"version\": \"1.0.0\",\n  \"description\": \"Repo containing some advanced patterns for react and for writing better React applications\",\n  \"main\": \"index.js\",\n  \"repository\": \"https://github.com/manjula91/react-advanced-patterns.git\",\n  \"author\": \"Manjula Dube\",\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"all-contributors-cli\": \"^5.4.1\"\n  },\n  \"scripts\": {\n    \"contributors:add\": \"all-contributors add\",\n    \"contributors:generate\": \"all-contributors generate\"\n  }\n}\n"
  },
  {
    "path": "prop-collection/README.md",
    "content": "# Prop Collections\r\n\r\n### What are prop collection?\r\n\r\nProp Collection is a pattern where we can access props from the parent component by passing a single object of props.\r\n\r\n### What is so special about this pattern?\r\n\r\nThis pattern allows us to spread the entire props required on the required component that is passed from the parent.\r\nThis ensures that the component receives all the props and we can also add our own props or override the current props.\r\n\r\n```jsx\r\n  getInputProps = {\r\n    type: \"text\",\r\n    onChange: this.onChange\r\n  };\r\n\r\n<input name=\"name\" {...getInputProps} />\r\n<input name=\"age\" {...getInputProps} />\r\n```\r\n\r\nI would prefer using Prop Getters to be more flexible since Prop Collection passes object instead of function which is not very flexible. Also there is a disadvantage for prop collection pattern: as we would need to pass the value externally. There is no way we can send it via props. So for eg you would also have to pass the event handler explicitly. Also if you want to add your own event handler along with the one provided by the parent, you need to extract the method from the parent and combine with your own handler which becomes rather messy.\r\n\r\n### Example\r\n\r\nHere's the codesanbox [example](https://codesandbox.io/s/yrjmqol7j) for prop collection\r\n"
  },
  {
    "path": "prop-getters/README.md",
    "content": "# Prop Getters\n\n### What are prop getters\n\nProp getters is a very unique pattern. This pattern involves the usage of spreading a function call in which we add our custom props and these are merged with the default props provided by the component.\n\n### What is so special about this pattern ?\n\nProp getters is a flexible pattern to provide props to the required component. This pattern is great when we want to add our own props without overriding the ones give by the main component.\n\nSuppose we have an input and we spread the props we obtained from the parent component like this:\n\n```jsx\n<input {...inputProps} />\n```\n\nNow these props we obtained include an onChange method for the input. What if we want to implement our own onChange method?\nThere is a way but it is messy\n\n```jsx\nconst {onChange, ...rest} = inputProps\n<input\n  {...rest}\n  onChange={() => {\n    onChange()\n    myOnChangeMethod()\n  }}\n/>\n```\n\nA better solution is to provide our method using prop-getters\n\n```jsx\n<input\n  {...getInputProps({\n    onChange: myOnChangeMethod\n  })}\n/>\n```\n\nNow it's upto the main parent component to provide a mechanism to call both the internal and supplied method.\nEasy!!!\n\n### Example\n\nBelow is an example shows how a Form component can manage state using prop-getters\n\n[Prop Getters in action](https://codesandbox.io/s/2pq87v9r4r)\n"
  },
  {
    "path": "provider-pattern/README.md",
    "content": "# Provider Pattern\n\n### What is the Provider Pattern\n\nProvider pattern is pattern where we can share data between multiple components. The main Component is responsible for data and state changes (Provider) and the child components receive the data via a special component (Consumer).\n\n### What is so special about this pattern\n\nThe best way to create this pattern is using React's new Context API. A method `createContext` is exposed by React from which you can create a Provider and a Consumer.\n\nThe problem is that when we want to pass data to components nested deep, we need to pass the props to each and every nested component, which becomes hard to refactor. The term coined for this is called **prop-drilling** (most probably by [Kent C. Dodds](https://twitter.com/kentcdodds)).\n\nThe Provider pattern solves this problem. In whichever component you want to access the data, just wrap it by the `Consumer` component which exposes all the state and methods via a render prop (yay!). This ensures that data stays where it is really required.\n\n### Example\n\nThis example shows a simple form component made using React's Context API.\n\n[Provider Pattern](https://codesandbox.io/s/p524z98zvx)\n"
  },
  {
    "path": "react-best-practices/README.md",
    "content": "# <h1 id=\"function-in-setstate\">Function in `setState` 🙌 ❤️</h1>\r\n\r\nThis is not a pattern but infact a best practice when interacting with your `state` in React.\r\n\r\nInstead of passing an object to `setState()`, pass a function instead. The function will be called with the current state of the component, even if the component itself has not been updated yet. The React Reconciler may merge concurrent state updates into a single lifecycle, which can create subtle and hard to find bugs in your code. In the case of a counter, a call like `this.setState({counter: this.state.counter++})` that happens on a user interaction may get fired 5 or 10 times before a React lifecycle occurs, which will lead to stale values in `this.state.counter`. **Values in this.state are not updated until React renders your component** and passing a function to `setState()` (e.g. `this.setState(state => ({counter: state.counter+1}))` will ensure your state is updated with the latest value instead of the latest _rendered_ value.\r\n\r\nFurther reading in [React's issue tracker](https://github.com/facebook/react/issues/11527)\r\n\r\n# <h1 id=\"why-did-you-update\"> Get rid of unnecessary updates in React</h1>\r\n\r\nIf you want to get rid of unnecessary rerenders and updates in React. Try [why-did-you-update](https://github.com/maicki/why-did-you-update)\r\n\r\nUsage:\r\n\r\n```\r\nimport React from 'react';\r\n\r\nif (process.env.NODE_ENV !== 'production') {\r\n  const {whyDidYouUpdate} = require('why-did-you-update');\r\n  whyDidYouUpdate(React);\r\n}\r\n```\r\nThis library is available on npm, install it with: ```npm install --save why-did-you-update``` or ```yarn add why-did-you-update```.\r\n\r\nWhy did you update is a function that monkey patches React and notifies you in the console when potentially unnecessary re-renders occur.\r\n\r\nHere's the [example](https://codesandbox.io/s/jnrx7j30vy)\r\n\r\nIn the example if you change your component.js to the below code. It will avoid unnecessary re-renders.That is, the component only needs to re-render when its props change. React has a special type of component built-in called PureComponent that is meant for exactly this use case.\r\n\r\nInstead of inheriting from React.Component, use React.PureComponent like this:\r\n\r\n```\r\nclass C extends React.PureComponent {\r\n  render() {\r\n    return <div>Age: {this.props.age}</div>\r\n  }\r\n}\r\n\r\n```\r\n\r\n# <h1 id=\"prop-spread\">Destructuring and spreading props</h1>\r\n\r\nThe ESNext Javascript has some great features and this is one of them. While passing props to a child component, we can spread the entire props we need to pass instead of passing them individually.\r\n\r\nInstead of this\r\n\r\n```jsx\r\nconst props = { a: 1, b: 2 }\r\n\r\n<MyComponent a={props.a} b={props.b} />\r\n```\r\n\r\nWe can do this\r\n\r\n```jsx\r\n<MyComponent {...props} />\r\n```\r\n\r\nMuch simpler and easier to read.\r\n\r\nThis pattern is also helpful if you want to exclude a single prop while passing the rest of them.\r\n\r\n```jsx\r\nconst props = { a: 1, b: 2, c: 3, d: 4 }\r\nconst {a, ...rest} = props\r\n\r\n<MyComponent {...rest} />\r\n```\r\n\r\nNow this component in render can destructure the props in this manner.\r\n\r\n```jsx\r\nclass MyComponent extends React.Component {\r\n  render() {\r\n    const { a, b } = this.props\r\n    return (\r\n      // JSX\r\n    )\r\n  }\r\n}\r\n```\r\n\r\n# <h1 id=\"prop-types\">Runtime type checking with prop types</h1>\r\n\r\nProp types are very important for checking the validity of props being passed to the component. Prop types help in determining whether the all the props have been passed correctly along with their data types at runtime. This helps identify issues in your component as well as you get prop information just by looking at the component file.\r\n\r\nProp types can be accessed by installing a package `prop-types`.\r\n\r\n```jsx\r\nimport React from 'react'\r\nimport PropTypes from 'prop-types'\r\n\r\nclass MyCompnent extends React.Component {\r\n  render() {\r\n    const { prop1, prop2 } = this.props\r\n    return (\r\n      <div>\r\n        <p>Prop 1: {prop1}</p>\r\n        <p>Prop 2: {prop2}</p>\r\n      </div>\r\n    )\r\n  }\r\n}\r\n\r\nMyComponent.propTypes = {\r\n  prop1: PropTypes.string,\r\n  prop2: PropTypes.string.isRequired\r\n}\r\n```\r\n\r\nHere we have mentioned that our component receives two props, both of which are strings and `prop2` is required. If we do not pass `prop2`, we will receive an error in the browser console.\r\n\r\nFor more details on prop types you can check out this link in the official React documentation\r\n[Typechecking with PropTypes](https://reactjs.org/docs/typechecking-with-proptypes.html)\r\n\r\n# <h1 id=\"default-props\">Using defaultProps</h1>\r\n\r\nDefault Props is a very useful feature as we can define props that do not need to be passed explicitly or when the prop has more than one value (enum type) and a single value will always be present regardless.\r\n\r\nDefault props just like prop-types can be set on any component like this\r\n\r\n```jsx\r\nconst MyComponent = ({ color }) => (\r\n  <div>\r\n    <p>Color: {color}</p>\r\n  </div>\r\n)\r\n\r\nMyComponent.propTypes = {\r\n  color: PropTypes.oneOf(['red', 'green', 'blue'])\r\n}\r\n\r\nMyComponent.defaultProps = {\r\n  color: 'red'\r\n}\r\n```\r\n\r\nNow the value of `color` prop will always be **red** whenever you don't pass it explicitly.\r\n\r\n# <h1 id=\"display-name\">Using displayName</h1>\r\n\r\nDisplay name is something that we should use when we create components as these are helpful in debugging.\r\n\r\nThe React DevTools shows the component name in the rendered component tree. This name is usually the name of the class in Class components\r\nFor example\r\n\r\n```jsx\r\nclass MyComponent extends React.Component { ... }\r\n```\r\n\r\nHere the component name `MyComponent` will be shown in the devTools. But for functional components defined like this, you should add a displayName for debugging purposes.\r\n\r\n```jsx\r\nconst MyComponent = props => { ... }\r\n\r\nMyComponent.displayName = 'My Component'\r\n```\r\n\r\nThis method is extremely useful when you are dealing with Higher Order Components because they provide a name that doesn't reflect the component you are wrapping.  Instead, you can modify and use the originally provided name to demonstrate that something was wrapped.\r\n\r\n```\r\nfunction connectToStore(storeName, Component) {\r\n  const componentName = Component.displayName || Component.name || 'anonymous'; // can't be sure the component is following best practices!\r\n  const name = `connectedTo${storeName}(${componentName})`;\r\n  \r\n  const wrapped = () => {\r\n     return <Component />;\r\n  };\r\n\r\n  wrapped.displayName = name;\r\n  wrapped.name = name;  \r\n\r\n  return wrapped;\r\n}\r\n\r\n```\r\n\r\nWith this example, calling `connectToStore('Authentication', UserDashboard)` will result in a component that appears in Dev Tools as `connectedToAuthentication(UserDashboard)`! \r\n\r\n# <h1 id=\"conditional-rendering\">Conditional Rendering</h1>\r\n\r\nAlso, when you only want to render an element on one condition, instead of doing this…\r\n\r\n```jsx\r\n{\r\n  isTrue ? <p>True!</p> : <none />\r\n}\r\n```\r\n\r\n… use short-circuit evaluation:\r\n\r\n```jsx\r\n{\r\n  isTrue && <p>True!</p>\r\n}\r\n```\r\n\r\nA note to consider, while conditionally rendering based on the length of an array, compare the length conditionally and then render.\r\n\r\nFor example, if you do this\r\n\r\n```jsx\r\n{\r\n  myArray.length && <p>True!</p>\r\n}\r\n```\r\n\r\nThis will render **0**. As 0 is falsy, the condition will short-circuit and because of that 0 will be rendered.\r\n\r\nInstead, do this\r\n\r\n```jsx\r\n{\r\n  myArray.length > 0 && <p>True!</p>\r\n}\r\n```\r\n\r\nOr this\r\n\r\n```jsx\r\n{\r\n  Boolean(myArray.length) && <p>True!</p>\r\n}\r\n```\r\n"
  },
  {
    "path": "render-props/README.md",
    "content": "# Render props\n\n### What are render props\n\nRender props are simply props that accept a function.\n\n### What is so special about this pattern\n\nRender props separate logic from the UI. The component that implements the logic doesn't decide how the UI is rendered, which is why I like this pattern a lot. The other reason is how easy it is to make the entire component declarative by just rendering what you need.\n\nWe can easily extend the API of our component and access only the logic required for rendering the UI. This makes render props more flexible as compared to Higher Order Components.\n\nAlso almost all patterns implemented using HOC's can be easily converted to the Render Prop pattern.\n\n### Example\n\nThis is a simple example of how the imperative fetch API can be converted into a declarative one (inspired by Apollo's Query component).\n\n[Fetch using render props](https://codesandbox.io/s/9yzwmkj7kp)\n"
  },
  {
    "path": "state-reducers/README.md",
    "content": "# State Reducer\n\n### What is a State Reducer\n\nState Reducer is a pattern where you can set the state of the component in a way similar to the reducer function in Redux.\n\n### What is so special about this pattern\n\nState Reducer pattern is pretty similar to the Redux reducer. It takes the previous state along with the current changes to be made in the state and merges them.\n\nThe main advantage of this pattern is that we can use the same components we have, without modifying the existing API and add a new way of updating state whilst keeping the UI separate from the logic (because of the widely loved render-props pattern). The reducer method which we write can be re-used in other components as we do not need to manage the state in our component.\n\nThis pattern stands out when all you want is to extend you API for state changes and keeping the current functionality untouched.\n\n### Example\n\nThis is an example of how a simple Form component can provide a different API altogether for creating a form.\n\n[Forms with State Reducers](https://codesandbox.io/s/9802yr86q4)\n\nHere is another example showing the use of multiple reducers for the state reduer pattern.\n\n[Multiple State Reducers](https://codesandbox.io/s/1o8o2xzm37)\n"
  }
]