How to use props to pass data around React components
Props are how a component obtains its properties. Starting from the top component, each child component gets its props from the parent component. In functional components, props are all the elements that are passed, and they can be added by addingprops
As a function parameter:
const BlogPostExcerpt = props => {
return (
<div>
<h1>{props.title}</h1>
<p>{props.description}</p>
</div>
)
}
In class components, props are passed by default. No need to add any special content, they can be accessed in the following waysthis.props
In the Component instance.
import React, { Component } from 'react'
class BlogPostExcerpt extends Component {
render() {
return (
<div>
<h1>{this.props.title}</h1>
<p>{this.props.description}</p>
</div>
)
}
}
Passing props to child components is a great way to pass values in an application. Components either hold data (have state) or receive data through their props.
It becomes complicated in the following situations:
- You need to access the state of the component from the children several levels down (all previous children need to act as transfer objects, even if they don’t need to know the state, it’s complicated)
- You need to access the state of the component from completely unrelated components.
Default value of props
If you don't need any value, if the default value is missing when you initialize the component, you need to specify a default value for it.
BlogPostExcerpt.propTypes = {
title: PropTypes.string,
description: PropTypes.string
}
BlogPostExcerpt.defaultProps = {
title: ‘’,
description: ‘’
}
Some tools, such asESLintAbility to force the definition of defaultProps for some components that do not explicitly require propTypes.
How to pass the props
When initializing the component, pass the props in a way similar to HTML attributes:
const desc = 'A description'
//...
<BlogPostExcerpt title="A blog post" description={desc} />
We pass the title as a pure string (you canas long asProcess strings! ), and use the description as a variable.
children
A special prop ischildren
. Included inbody
The name of the component, for example:
<BlogPostExcerpt title="A blog post" description="{desc}">
Something
</BlogPostExcerpt>
under these circumstancesBlogPostExcerpt
We can access "something" by looking upthis.props.children
.
Although props allow a component to receive properties from its parent object, such as being "instructed" to print some data, the state allows the component to take on its own life and be independent of the surrounding environment.
Download mine for freeResponse Handbook
More response tutorials:
- A simple React application example: Get GitHub user information through API
- Build a simple counter with React
- VS Code setup for React development
- How to pass props to child components through React Router
- Create an application using Electron and React
- Tutorial: Create a spreadsheet with React
- Roadmap for learning React
- Learn how to use Redux
- Getting started with JSX
- Stylized components
- Introduction to Redux Saga
- Introduction to React Router
- Introduction to React
- Reaction component
- Virtual DOM
- Reaction event
- Reaction state
- Reaction props
- Reaction fragment
- React Context API
- Reaction PropTypes
- Reaction Concept: Declarative
- React: How to display other components when clicked
- How to loop inside React JSX
- Props and status in React
- Should you use jQuery or React?
- How much JavaScript do I need to know to use React?
- Gatsby Introduction
- How to reference DOM elements in React
- One-way data flow in React
- React high-end components
- React to life cycle events
- Reaction concept: immutability
- Reaction concept: purity
- Introduction to React hooks
- Introduction to create-react-app
- Reaction concept: composition
- React: demo component and container component
- Code splitting in React
- Server-side rendering with React
- How to install React
- CSS in React
- Use SASS in React
- Processing forms in React
- Reaction strict mode
- Reaction portal
- React rendering props
- Test React components
- How to pass parameters to event handlers in React
- How to deal with errors in React
- How to return multiple elements in JSX
- Conditional rendering in React
- Reaction, how to transfer props to subcomponents
- How to get the value of an input element in React
- How to use useState React hook
- How to use useCallback React hook
- How to use useEffect React hook
- How to use useMemo React hook
- How to use useRef React hook
- How to use useContext React hook
- How to use useReducer React hook
- How to connect your React app to the backend of the same source
- Reaching the router tutorial
- How to use React Developer Tools
- How to learn React
- How to debug a React application
- How to render HTML in React
- How to fix `dangerouslySetInnerHTML` does not match the error in React
- How can I solve the problem of React login form status and browser auto-fill
- How to configure HTTPS in React application on localhost
- How to fix the "Component cannot be updated while rendering other components" error in React
- Can I use React hooks within the conditions?
- Using useState with objects: how to update
- How to move in code blocks using React and Tailwind
- React, focus on an item in React when added to the DOM
- Response, edit text on doubleclick