DEV Community

loizenai
loizenai

Posted on

React Component Props example

https://grokonez.com/frontend/react/react-component-props-example

React Component Props example

In this tutorial, we're gonna look at how to use React Component Props in a React example.

Related Posts:

I. React Component Props

When we need to pass immutable data to a component, we use props.

1. Props with Class Component

We use this syntax to pass data as a props element to a class template:

<MyClass element={data} />

To get data from props, use this.props.data.
For example:

class NoteApp extends React.Component {
    render() {
        const jsaDescription = {
            'd1': 'Java/JavaScript Technology',
            'd2': 'Spring Framework'
        }

        return (
            <div>
                <Header description={jsaDescription} />
            </div>
        );
    }
}

class Header extends React.Component {
    render() {
        return <h4>{this.props.description.d1} - {this.props.description.d2}</h4>;
    }
}

2. Props with Function

class NoteApp extends React.Component {
    render() {
        const jsaTitle = 'Java Sample Approach';

        return (
            <div>
                <Title title={jsaTitle} />
            </div>
        );
    }
}

function Title(props) {
    return <h2>{props.title}</h2>;
}

II. Practice

1. Overview

react-component-props-example-overview

Using props, we will pass:

  • title to Title element
  • description to Header component
  • notes array to Notes component, then each item of notes array to each Note component

More at:

https://grokonez.com/frontend/react/react-component-props-example

React Component Props example

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay