Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic attribute in ReactJS

Tags:

reactjs

I want to dynamically include/omit the disabled attribute on a button element. I have seen plenty of examples of dynamic attribute values, but not of attributes themselves. I have the following render function:

render: function() {     var maybeDisabled = AppStore.cartIsEmpty() ? "disabled" : "";     return <button {maybeDisabled}>Clear cart</button> } 

This throws a parse error because of the "{" character. How can I include/omit the disabled attribute based on the (boolean) result of AppStore.cartIsEmpty()?

like image 506
Timmy Avatar asked Mar 17 '15 15:03

Timmy


People also ask

How do you set a dynamic attribute in React?

By using spread attributes, you can dynamically add (or override) whatever attributes you'd like by using a javascript object instance. In my example above, the code creates a disabled key (with a disabled value in this case) when the disabled property is passed to the Hello component instance.

What is a dynamic attribute?

Dynamic attributes represent changing characteristics of the resource. Dynamic attributes of a host resource, for example, would identify such things as the average number of processes that are waiting in the run queue, processor idle time, and the number of users who are currently logged on.

What is dynamic in React JS?

As the building blocks of React applications, components are dynamic, in that they can describe a template of HTML and fill in variable data. This lesson builds a real example of a blogging application to illustrate dynamic components.

How do I add a dynamic tag in React?

You can simply pass the dynamic tag name as the first argument to the React. createElement() method (as it accepts a string tag name). For example: const type = (someCondition) ?


2 Answers

The cleanest way to add optional attributes (including disabled and others you might want to use) is currently to use JSX spread attributes:

var Hello = React.createClass({     render: function() {         var opts = {};         if (this.props.disabled) {             opts['disabled'] = 'disabled';         }         return <button {...opts}>Hello {this.props.name}</button>;     } });  React.render((<div><Hello name="Disabled" disabled='true' />     <Hello name="Enabled"  /> </div>) , document.getElementById('container')); 

By using spread attributes, you can dynamically add (or override) whatever attributes you'd like by using a javascript object instance. In my example above, the code creates a disabled key (with a disabled value in this case) when the disabled property is passed to the Hello component instance.

If you only want to use disabled though, this answer works well.

like image 193
WiredPrairie Avatar answered Sep 17 '22 22:09

WiredPrairie


You can pass a boolean to the disabled attribute.

render: function() {     return <button disabled={AppStore.cartIsEmpty()}>Clear cart</button> } 

function Test() {   return (     <div>       <button disabled={false}>Clear cart</button>       <button disabled={true}>Clear cart</button>     </div>   ); }  ReactDOM.render(<Test />, document.querySelector("#test-container")); console.log(Array.from(document.querySelectorAll("button")));
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <div id="test-container"></div>
like image 26
Alexandre Kirszenberg Avatar answered Sep 17 '22 22:09

Alexandre Kirszenberg