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()?
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.
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.
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.
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) ?
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With