2

I have an input field which should be readonly based on some state change. So I have written the JSX like below:

render: function() {
var reo = 'readonly';
return (<input id="output" type="Text" value="0" {reo}/>);
}

In the above code variable reo is hardcoded but in the real scenario it will be evaluated based on some condition like:

var reo = (isDisabled ? 'readonly' : '');

When I am running this it is giving unexpected token error. Whereas when I am writing JSX like this:

<input id="output" type="Text" value="0" readonly/>

it is working fine. Please explain me why this error is occurring and how to correct it.

1 Answer 1

2

The problem in the example is that you pass a string while react expects a prop with the name readonly.

The following should work:

render: function() {
  var reo = true;
  return (
    <input id="output" type="Text" value="0" readonly={reo} />
  );
}

Or using isDisabled:

render: function() {
      return (
        <input id="output" type="Text" value="0" readonly={isDisabled} />
      );
    }
Sign up to request clarification or add additional context in comments.

3 Comments

You must specify why the code of OP is not working and why your code will work.
Not sure what must means... :) I am volunteering here :) The problem in the example is that you pass a string while react expects a prop with the name readonly...
Yup, I know, that's what I am saying. Just post some details about it in answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.