This method works well, but I want to follow the code method of the project I referenced.

clock Icon

asked 120 days ago Asked

message

0 Answers

views

10 Views

s 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 <input classname="foo" {isRequired ? "required" : ""} />
}

 

May be one comment help someone, i found out React 16.7 doesnt rerenders and update the component's html attributes if you changed only them in a store (f.e. redux) and tied to component. This means the component has f.e.aria-modal=true, you push the changes (to false) to the store of aria/data attributes, but nothing else is changed (such as component's content or class or variables in there) as the result ReactJs will not update aria/data attrs in that components. I've been messing around about whole day to realise that.

 

 

Update: if anyone is curious as to how/why this happens, you can find details in ReactDOM's source code, specifically at lines 30 and 167 of the DOMProperty.js file.

 
 

0 Answers

Write your answer here

Top Questions