Languages
[Edit]
EN

React - how to pass props to react components

3 points
Created by:
Eshaal-Wilkinson
774

In this article, we would like to show different ways how to pass props into React components.

Note: in the below example we use function components to show main idea.

Values can be passed as props using:

  1. single or double apostrophes syntax ('' or "") - that let us use only string values,
  2. curly brackets syntax ({}) that let us pass:
    • values with different types,
    • variables,
  3. spread syntax (...) that syntax that reduces the time needed to create a new component - read more about spread syntax here.

Quick solution: 

Read below sections to see how different cases work in details.

Practical examples

Edit

1. Single or double apostrophes example

Edit

JSX lets us use '' or "" when we pass props values. The main idea of this approach is to simplify string-valued props usage.

So component creation should look like:

Practical example:

 

2. Curly brackets example

Edit

That approach lets us pass values with different types as props. Each prop can get value from literal or some variable.

So component creation should look like:

Where age={'25'} is equal to age="25" syntax.

Practical example:

 

3. Spread syntax example

Edit

That case let us avoid typing many object props one after one and pass them using one operation.

So component creation should look like:

Practical example:

 

Alternative titles

  1. React - pass object as props
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - how to pass props
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join