简体   繁体   English

反应意外的令牌,预期为“…”

[英]React Unexpected token , expected “…”

What am I doing wrong in this code, I don't know how else to write it I want to test the state invalidForm and render disabled or null depending. 我在这段代码中做错了什么,我不知道该怎么写,我想测试状态invalidForm并根据其呈现禁用或空值。

state code UPDATE 状态码UPDATE

if(valid) {
        return true;
    } else {
      this.setState({
        invalidForm: true
      })
    }
}


<button {this.state.invalidForm ? 'disabled' : null} type="submit">Submit</button>

The Error I am getting is 我得到的错误是

 Unexpected token, expected "..." 

While you can define props without a value , you cannot do that dynamically. 尽管您可以定义没有值的道具 ,但是您不能动态地做到这一点。 Pass a value: 传递值:

<button disabled={this.state.invalidForm}>Submit</button>

It shouldn't matter, but for clarity, if this.state.invalidForm is not a Boolean value, you can convert it to one: 没关系,但为清楚起见,如果this.state.invalidForm不是布尔值,则可以将其转换为一个:

<button disabled={Boolean(this.state.invalidForm)}>Submit</button>

Running example: 运行示例:

 ReactDOM.render( <div> <button disabled={true}>Button 1</button> <button disabled={false}>Button 2</button> </div>, document.body ); 
 <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> 


The parser expects ... because the {} syntax inside an opening "tag" is reserved for "spreading" props from objects. 解析器期望...因为开头的“标记”中的{}语法保留用于从对象“传播”道具。

For example: 例如:

const props = {disabled: true};
return <button {...props}>Submit</button>

is the same as 是相同的

return <button disabled={true}>Submit</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM