[英]React: Parsing error: Unexpected token, expected “…”
So I'm inside my render()
-method creating some <select>
. 所以我在我的
render()
方法内部,创建了一些<select>
。 I want to select a default value but only when it's not a <select multiple>
. 我想选择一个默认值,但仅当它不是
<select multiple>
。 I tried this but encountered the error in the title. 我尝试了此操作,但在标题中遇到了错误。 How would I code this correctly?
我该如何正确编码?
<select key={setting.id} multiple={setting.multiple} {setting.multiple ? '' : 'value={setting.default || ""}'}><select>
^ error occurs here
EDIT: 编辑:
It is important that the select has no value-attribute at all in the case it is a <select multiple>
to mitigate the The value prop supplied to <select> must be an array if multiple is true.
重要的是,如果选择为
<select multiple>
,则选择根本不具有值属性,以减轻影响The value prop supplied to <select> must be an array if multiple is true.
值属性The value prop supplied to <select> must be an array if multiple is true.
-warning. -警告。
Something like below may solve your problem: 如下所示可能会解决您的问题:
render(){
let selValue = "";
if(setting.multiple)
val = setting.default;
return(
<select key={setting.id} multiple={setting.multiple} value={selValue}><select>
)
}
EDIT: According to your needs you can also use below code: 编辑:根据您的需要,您还可以使用以下代码:
let select = (<select key={setting.id} multiple={setting.multiple} value={setting.default}><select>)
if(setting.multiple)
select = (<select key={setting.id} multiple={setting.multiple}><select>)
In JSX, the {}
do not output strings as is the case in many templating languages, you need to look at it as an assignment where the left hand is the property, eg multiple=
, and the right hand is a JavaScript expression that returns a value to be assigned, eg { setting.multiple }
- it gets evaluated and assigned to the multiple
property. 在JSX中,
{}
不会像许多模板语言一样输出字符串,您需要将其视为一个赋值,其中左手是属性,例如multiple=
,右手是一个JavaScript表达式,该表达式返回一个要分配的值,例如{ setting.multiple }
-它被求值并分配给multiple
属性。
Now to set the value depending on the setting
prop just follow the same logic: 现在根据
setting
道具设置值,只需遵循相同的逻辑即可:
<select value={ setting.multiple ? [] : (setting.default || "") } />
EDIT: to explain why the error says expected "..."
. 编辑:解释为什么错误说
expected "..."
。 If the {}
do not fall on the right side of an assignment it is expected that you want to spread multiple properties, for example 如果
{}
不在工作分配的右侧,则可能需要分布多个属性,例如
const props = { value: "foo", name: "myInput" }; <select {...props } />
const props = { value: "foo", name: "myInput" }; <select {...props } />
. const props = { value: "foo", name: "myInput" }; <select {...props } />
。
How about doing it this way? 这样做怎么样?
setting.multiple
? <select key={setting.id} multiple={setting.multiple}></select>
: <select key={setting.id} multiple={setting.multiple} value={setting.default || ""}</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.