React Design Patterns and Best Practices(Second Edition)
上QQ阅读APP看书,第一时间看更新

Boolean attributes

A couple more things are worth mentioning before starting for real regarding the way you define Boolean attributes in JSX. If you set an attribute without a value, JSX assumes that its value is true, following the same behavior of the HTML disabled attribute, for example.

This means that if we want to set an attribute to false, we have to declare it explicitly as false:

  <button disabled /> 
React.createElement("button", { disabled: true });

The following is another example:

  <button disabled={false} /> 
React.createElement("button", { disabled: false });

This can be confusing in the beginning, because we may think that omitting an attribute would mean false, but it is not like that. With React, we should always be explicit to avoid confusion.