Author avatar

Gaurav Singhal

How to Pass Values Inside Attributes in React

Gaurav Singhal

  • Aug 19, 2020
  • 6 Min read
  • 473 Views
  • Aug 19, 2020
  • 6 Min read
  • 473 Views
Web Development
Front End Web Development
Client-side Framework
React

Introduction

An attribute is a property of an element used to provide access to additional data required for that specific element to process the output. Each element in HTML consists of several attributes that act as a property for that element, followed by the appropriate value.

An attribute with a custom value is required when you want to alter the behavior of an element; hence, in React, you can use an in-built attribute of a component or create a custom attribute for the child component.

In this guide, you will learn how to pass static and dynamic values to the attribute and create a custom attribute for the child components.

Pass Static Values to an Attribute

Any attribute associated with the UI element needs the value to make it work. Thhe value can be anything, that is, static or dynamic.

If you have a fixed value for an element, then it calls static values that are stored in variables, and once you need to use that value, it consumes directly.

For example, say you have the <img> element in your React component, and the URL is stored in a state variable, as shown below.

1
2
3
4
5
6
constructor() {
    super();
    this.state = {
      imageUrl: "your_image_url",
    };
}
jsx

You need to place one state variable inside the local state of the component imageUrl3, where you can put an appropriate path or URL of the image.

The state variable can be consumed directly with the element, as shown below.

1
2
3
4
5
6
7
8
render() {
    console.log(this.props)
    return (
      <div>
        <img src={this.state.imageUrl} />
      </div>
    );
}
jsx

As you can see, this.state.imageUrl is accessed from the local state; hence, it’s called dynamic value to the src attribute and the imageUrl as a static value.

In the same way, it can also be applied to different elements, and a few examples are shown below.

1
2
3
4
5
6
7
8
9
10
11
render() {
    console.log(this.props)
    return (
      <div>
        <a href={this.state.linkUrl} target="_blank">
          Hit me to reach Google
        </a>
        <input type="text" value={this.state.inputValue} />
      </div>
    );
}
jsx

Along with the <a> attribute, the static value used is this.state.linkUrl, and with the <input> control, this.state.inputValue is used as a static value to the attribute.

Pass Dynamic Values to an Attribute

So far in this guide, you have learned how to pass static values to an attribute. But that may require you to use frequently changed values along with the attribute.

For example, you may have one text input, and its element’s value updates as you change its value. The example below demonstrates this.

Create one state variable with a default value.

1
2
3
4
5
6
constructor() {
    super();
    this.state = {
      fullName: "test"
    };
}
jsx

Now create the <input> element along with the required attributes.

1
2
3
4
5
6
7
8
9
10
11
12
render() {
    return (
      <div>
        Enter full name :{" "}
        <input
          type="text"
          value={this.state.fullName}
          onChange={this.onValueChange}
        />
      </div>
    );
}
jsx

In the above example, the game-changer props are onChange(), used to update the existing value with the updated value.

1
2
3
onValueChange = event => {
    this.setState({ fullName: event.target.value });
};
jsx

So, once you update the existing value in input control, it will be updated into the state, and the same value will appear in input control using the props, as shown below.

1
value={this.state.fullName}
jsx

By consuming and updating the different state values, you can provide dynamic values to the element’s attribute.

Pass Values to an Event Attribute

Events are handy when user interaction is required in the existing page; thus, values can also be passed to an event attribute as with the in-built attributes of the elements, such as src, href, value, height, width, and so on.

You can define the values anywhere, such as the local state in the components, to pass the values to the event attribute. See below.

1
2
3
4
5
6
7
8
9
constructor() {
    super();
    this.state = {
      student: {
        name: "heyfoo",
        age: "18"
      }
    };
}
jsx

Inside the state object of the component, student is a custom object created along with the key-value pair.

The next step is to implement an event attribute, such as onClick().

1
2
3
4
5
6
7
8
9
render() {
    return (
      <div>
        <span onClick={() => this.setObjectValue(this.state.student)}>
          Send object
        </span>
      </div>
    );
}
jsx

Inside the <div>, there is one element created, <span>, along with the event attribute, called onClick, which triggers the event when the user clicks on the span element.

Once the user clicks on the element, you can pass any custom values as a function argument.

1
onClick={() => this.setObjectValue(this.state.student)}
jsx

this.setObjectValue is a custom function that accepts one argument, and the static argument given is this.state.student.

To access the object from the event function, see below.

1
2
3
setObjectValue(obj) {
  console.log(obj);
}
jsx

You can pass any static or dynamic values along with the event function, just like in the above example.

Conclusion

Attributes play an essential role when using HTML elements with static or dynamic values to achieve interactive results.

I hope this guide helps you to implement various ways to provide value to an attribute.

0