Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

ReactJs Properties

Home  »  React   »   ReactJs Properties
Posted on July 29, 2022July 29, 2022 No Comments on ReactJs Properties
503

A component props is an object that holds information about that component. They are similar to attributes in HTML. In fact, they are passed to the components the same way attributes are passed to HTML.

Here’s the code for the component we made in my previous article.

import React from "react";
import ReactDOM from "react-dom";

class Greeting extends React.Component {
    render() {
    return (
        <div className = "box">
        <h2> Hello Human Friend!!!</h2>
        <p> We are so glad to have you here. </p>
        </div>
    );
    }
}

ReactDOM.render(<Greeting />, document.getElementById("app"));

Adding Properties to Components

So let’s say we want to pass a value to the name property of Greeting the component we created. We will do this.

<Greeting name = "TeachDeveloper" />

Quite simple right? A component can take as many properties as you want it to.

<Greeting name = "TeachDeveloper" message = "I will like you to be my friend" blah = "blah blah blah" />

Take note of what’s necessary to pass property. You need a name that in the above example is name message and blah. The name of the property can be anything you choose to call it. The second thing is the information you want that property to store. These are “TeachDeveloper”, “I will like you to be my friend” and “blah blah blah” in the example above. If we want to pass information that’s not a string, say a function, an object, an array, or a variable, it has to be wrapped in curly braces.

<Greeting message = {greetings} myInfo = {{firstName:"Teach", lastName:"Developer" }} />

How can we access and display this property we just passed to the component?

Accessing Properties

We use this.propsto access properties passed to a component. The this.props object contains all properties that a component has. Let’s go back to our example of the Greeting component. Let’s say we want the user to be able to pass the name of the person to say hello to and the message to be passed. This is how we will do that.

class Greeting extends React.Component {
    render() {
        return (
            <div className = "box">
                <h2> Hello {this.props.name}!!!</h2>
                <p> {this.props.message} </p>
            </div>
        );
    }
}

What we have just done is to tell the component to render whatever information is passed to the name property and message property. So if we do this

ReactDOM.render(<Greeting name = "Teachdeveloper" message = "I want to be your friend" /> , document.getElementById("app"));

This is what we are actually rendering

...
<div className = "box">
    <h2>Hello Teachdeveloper!!!</h2>
    <p>I want to be your friend</p>
</div>
...

Handling an Event

There are times when we want to pass functions to a property. For instance, you want a function to handle an event like onClick or onKeyDown, how can this be done? Let’s proceed with our example to explain this. Let’s add a button to our Greeting that says “hello” when clicked.

First of all, we will add a method to the Greeting component that handles the onClick event. This method will be outside render().

class Greeting extends React.Component {

    handleClick () {
        alert ("Hello Human");
    }
    
    render() { ...

Then we will pass the method to the onClick property of the button we add.

...
    render () {
        return (
            <div className = "box">
            <h2>Hello {this.props.name}!</h2>
            <p>{this.props.message}</p>
            <button onClick = {this.handleClick}>Click Me</button>
            </div>
            );
    }

Default Properties

You can also pass default properties to a component. For instance, what if a person uses the Greeting Component without passing any property to it? We don’t want our new acquaintances to think we are unfriendly. In this case, we can set the default props for the component. We do this by adding a default value property for our component. This defaultProps must be equal to an object.

Greetings.defaultProps = {name : "Human Friend", message : "You are welcome to our world"};

This will be added after the class declaration.

Accessing the Children

Components can also have children. For instance, the `Greeting` component can be written as

<Greeting>Hello</Greeting>

This is still valid. To access the children of this component, which in this case is the string Hello, we use this.props.children. With this, we can access anything that’s between the two tags.

NOTE One last thing to note is that Properties are read-only. A component should not modify its own props. props store information that can only be modified by another component and never by itself.

Here’s a Codepen that demonstrates all we’ve done so far. Try to play with it until you are sure you understand what we’ve done.

Got any questions? Any addition? Feel free to leave a comment.

Thank you for reading

React Tags:React

Post navigation

Previous Post: Understanding State in React Components
Next Post: Important most things you should know about JSX

Related Posts

  • How to Use useLocation Hook in React Router DOM V6
  • How to Deploy or Host your ReactJS App in cPanel
  • Destructuring Assignment in ES6- Arrays
  • How to clear your cache in npm
  • Understanding State in React Components
  • ES6 classes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to change the link color of the current page with CSS?
How to Use useLocation Hook in React Router DOM V6
How to make the scrollbar visible in mobile browsers with CSS?
How to Deploy a React application on a cPanel
How to check if a customer is logged in to Magento 2 or not?

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved