Component props

As I look at this code, I see that not a lot of it is Pokemon specific. Really, this pager is pretty generic, except for this hard-coding of Pokemon here. It would be really nice if I could take this component that I want to render as a prop. In fact, I can. In JSX, I can use this.props.component. Now, we haven’t set up our component to take that, so let’s do that now. Here, we’ll provide that component. Now, that might feel like voodoo, the fact that just worked like that, so I’d like to use another component to illustrate a little bit of how it works, and how we can use it to compose things and make components like this Pokemon pager more generic. I’ll make a very simple, functional component called ShowId. Like Pokemon, it’s going to take an Id on props and just render it as big as it can. Now when we render a Pokemon pager we can give it any component that we want, so why not ShowId. Now it’s just showing the Id that it gets when Pokemon pager renders it here. Now, let’s say that we don’t want to definea component at all. Well we can just take this and shove it inside our component, because components are again just functions. We’ll take the props, and then render this h1 with props.id, and as you’ve seen, this totally works. So all of these work. We’re going to instead use our Pokemon, and we’re going to strip Pokemon out of the name here, because honestly this is just a generic IdPager now. Now we’ve used composition to insert this very specific component into this generic component. This is super awesome, and we’re going to learn more ways to do that in the next lessons.

Play video: Component props
  1. Course overview
  2. Project setup
  3. ReactDOM.render and React.createElement
  4. JSX
  5. Functional components
  6. Props
  7. Class Components
  8. onClick and other events
  9. Component initial state
  10. setState - object form
  11. setState - functional form
  12. Conditional rendering in JSX
  13. essential react 13
  14. Fetching pokeapi data
  15. componentDidMount
  16. setState and fetch
  17. Lists with Array.map()
  18. componentWillReceiveProps
  19. 👉Component props
  20. Render props
  21. Separation of Concerns with components
  22. Higher-order components
  23. defaultProps
  24. Render prop actions
  25. Course summary