循环遍历字符串数组并将其渲染为 React 组件中的 Prop
类似的问题要么无关紧要,要么很复杂。我提到了这个SO线程但仍然没有得到我想要的输出。
所需的输出:
Hi there, Anna!
Hi there, Je!
Hi there, Ram!
我尝试使用 .map()
但只导致没有输出。
这是我写的代码:
import React from 'react';
import ReactDOM from 'react-dom';
class Greeting extends React.Component {
render() {
return <h1>Hi there, {this.props.firstName}!</h1>;
}
}
const names = ["Anna", "Je", "Ram"];
const greet_em = names.map(name => (<Greeting firstName={name}/>));
ReactDOM.render(
{greet_em},
document.getElementById('app')
);
Questions similar to this were either irrelevant or complicated. I've referred to this SO thread but still not getting my desired output.
Desired output:
Hi there, Anna!
Hi there, Je!
Hi there, Ram!
I've tried playing around with .map()
but only resulted in no output.
Here is the code I've written:
import React from 'react';
import ReactDOM from 'react-dom';
class Greeting extends React.Component {
render() {
return <h1>Hi there, {this.props.firstName}!</h1>;
}
}
const names = ["Anna", "Je", "Ram"];
const greet_em = names.map(name => (<Greeting firstName={name}/>));
ReactDOM.render(
{greet_em},
document.getElementById('app')
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保单独组件的 JSX 包装在包含元素中。
Make sure your separate components' JSX are wrapped within a containing element.