Is there a way to only add attributes to a React component if a certain condition is met?
I’m supposed to add required and readOnly attributes to form elements based on an Ajax call after render, but I can’t see how to solve this since readOnly=”false” is not the same as omitting the attribute completely.
The example below should explain what I want, but it doesn’t work.
(Parse Error: Unexpected identifier)
function MyInput({isRequired}) {
return
}
For example, in some ways, React is intelligent enough to ignore the attribute if the value you pass to is not true: const InputComponent = function() constant = true; constable disabled = false; .\n\nReturn (in type=\”text\” disabled=disabled required =required /> ); .\n\nparaphrase: will cause: input type=\”text\” required> .\n\nUpdate: If anyone is interested in how/why this is the case, you can obtain details in ReactDOM\’s source code, particularly at lines 30 and 167 of the DOMProperty.js file.