React Components: Difference between revisions
No edit summary |
|||
Line 33: | Line 33: | ||
export default Page | export default Page | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Replacing with Components== | |||
To start making our components we can replace the images with components. For example | To start making our components we can replace the images with components. For example | ||
<syntaxhighlight lang="js"> | <syntaxhighlight lang="js"> | ||
Line 54: | Line 55: | ||
export default Page | export default Page | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Component Abstractions== | |||
In programming three patterns used to abstract | |||
* HOC - Higher Order Component | |||
A higher-order component is a function that takes a component and returns a new component | |||
* RP - Render Prop | |||
A RP is simply a prop that takes a function which returns elements that will be used in render(). | |||
You can pass an element directly into a prop and use it in render() which would make the whole thing a RP by name, but generally, when people speak about RPs, they mean the first definition. | |||
* Context | |||
Context in React is used to share data that is global to a component tree such as an authenticated user or preferred theme without passing that data as props. |
Revision as of 03:28, 4 December 2020
Designing Components
Resources
These can found here https://github.com/pkellner/pluralsight-designing-react-components-course-code Implements
- Component Reuse
- Single Responsibility
- Dont Repeat Yourself
Next JS Setup
Create project with
npm install react react-dom next --save
Add three commands to packages.json
"dev": "next",
"build": "next build",
"start": "next start"
Basic Page
With next js we can create a basic page using images to represent components
function Page() {
return (
<div>
<img src="images/header.png" />
<img src="images/menu.gif" />
<img src="images/searchbar.gif" />
<img src="images/speakers.png" />
<img src="images/footer.png" />
</div>
)
}
export default Page
Replacing with Components
To start making our components we can replace the images with components. For example
import React from 'react'
const Header = () => <img src="images/header.png" />
export default Header
And replace the Page with the components i.e.
function Page() {
return (
<div>
<Header />
<Menu />
<SpeakerSearchBar />
<Speakers />
<Footer />
</div>
)
}
export default Page
Component Abstractions
In programming three patterns used to abstract
- HOC - Higher Order Component
A higher-order component is a function that takes a component and returns a new component
- RP - Render Prop
A RP is simply a prop that takes a function which returns elements that will be used in render(). You can pass an element directly into a prop and use it in render() which would make the whole thing a RP by name, but generally, when people speak about RPs, they mean the first definition.
- Context
Context in React is used to share data that is global to a component tree such as an authenticated user or preferred theme without passing that data as props.