[英]Pass in parameter to a react component when rendered from a method
I am playing with React.js (I am new to it) and had a strange issue. 我正在玩React.js(我是新手),但是遇到了一个奇怪的问题。
I can render a sub-component from the parent's render. 我可以从父级渲染器渲染子组件。 This works: 这有效:
<Station id="1" label="Start" />
I tried to extract this to a method: 我试图将其提取为一种方法:
renderStation(id, label) {
return <Station id="{id}" label="{label}" />
}
// call it from the render() as
{this.renderStation(1, "Start")}
Strangely it passes in the strings "{id}" and "{label}" as props instead of "1" and "Start". 奇怪的是,它以字符串“ {id}”和“ {label}”作为道具,而不是“ 1”和“ Start”。 Rendering a <div>{id}</div>
from the same method just works fine. 用相同的方法渲染<div>{id}</div>
可以正常工作。 What is wrong? 怎么了?
See my playground with the repro here: https://jscomplete.com/repl?j=HybEKZuub 在这里查看带有repro的游乐场: https ://jscomplete.com/repl ? j = HybEKZuub
请删除双引号,您可以开始使用。
<Station id={id} label={label} />
To Avoid confusion.. 为了避免混乱。
let props = {
"id":id,
"label":label
}
<Station {...props} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.