0
render(){
  const { loading } = this.state;
  return(
    <div>
      {!loading ? <input disabled type='text' /> : <input type='text' />}
    </div>
  )
}

Above jsx make sense? I didn't get any compliation error, just that I got a warning from react saying Unknown propdisabbedon <input> tag.

How to changed the attr of the button to disabled the correct way? Imagine if my input has lots of class of css, do I need to repeat them too? I felt it's redundant.

2
  • 1
    <input disabled={loading} type='text' /> Commented Apr 9, 2017 at 10:36
  • Note that you are rendering a text field actually. 😉 Commented Apr 9, 2017 at 11:46

1 Answer 1

2

You don't need a conditional rendering on the input tag. You can do it the following way

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true
    }
  }
  render(){
    const { loading } = this.state;
    return(
      <div>
        <input disabled={loading} type='text'/>
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

Sign up to request clarification or add additional context in comments.

3 Comments

Note that using constructor to set the initial state is discouraged, also, when you use super, you should pass the ...props to it as well in case you are going to use this.props in the same context. Better to use the class properties syntax. (state = { ...stuff })
@FezVrasta I agree with you that props should be passed to the constructor and super otherwise it can lead to potential bugs. However why is it discouraged to set state constructor. see facebook.github.io/react/docs/… and
The doc makes use of the constructor because it's more widespread, but it leads to more bugs because of what I explained. You can refer to this.props inside a class property and avoid any super quirkiness. gist.github.com/FezVrasta/630e92170d20618fc35d55c83fefcd2d

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.