简体   繁体   English

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

[英]Unexpected token, expected "..." React

I want to put "defaultChecked" attribute to JSX input element but the compiler requires me to use spread operator for activeModal state?我想将“defaultChecked”属性放入 JSX 输入元素,但编译器要求我对activeModal状态使用扩展运算符? Why is that?这是为什么? The state is either true or false:状态为真或假:

<input name="radio-size"value="small" type="radio" id="small" {**activeModal**?"checked":""} className="modal-content-sizes-form-option"></input>

JSX expects, that if you put an an expression inside {} somewhere that it expects a prop name, then that expression will be ...someObject where someObject contains a props to values mapping. JSX 期望,如果您在{}放置一个表达式,它需要一个道具名称,那么该表达式将是...someObject ,其中someObject包含一个道具到值的映射。

eg例如

const myObject = {
    name: "radio-size",
    value: "small",
    // etc
}

<input {...myObject} />

It doesn't expect a string of JSX to insert.它不希望插入一串 JSX。

If you want to set a boolean prop, then just assign boolean to the prop:如果你想设置一个布尔道具,那么只需将布尔值分配给道具:

<input checked={activeModal} (your other props) />

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

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