最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to render disabled button by if statement? - Stack Overflow

programmeradmin0浏览0评论

I want to disable a button in a react ponent when a state of checkedIds is empty, the code to disable button is just like this:

<Button disabled> disabled </Button>

But when I try this it don't work

<Button {if(this.state.checkedIds.length===0) {
    return disabled;
  }}>
      Delete Selected 
  </Button>

Help?

I want to disable a button in a react ponent when a state of checkedIds is empty, the code to disable button is just like this:

<Button disabled> disabled </Button>

But when I try this it don't work

<Button {if(this.state.checkedIds.length===0) {
    return disabled;
  }}>
      Delete Selected 
  </Button>

Help?

Share Improve this question asked Jun 7, 2017 at 16:15 gpbaculiogpbaculio 5,96814 gold badges68 silver badges112 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

If-else doesn't directly work like that, you should make use of ternary operator and return a boolean value like

<Button disabled={(this.state.checkedIds.length == 0? true: false)}>
      Delete Selected 
  </Button>

This should work.

<Button disabled={this.state.checkedIds.length===0} >
  Delete Selected 
</Button>
发布评论

评论列表(0)

  1. 暂无评论