reactjs - 在反应 jsx 元素数组值中添加新行
问题描述
在反应中显示值时尝试添加新行。
有人可以告诉我我错过了什么吗?
这是我的代码
const qList = qs.map(
(question, idx) => (<div key={idx}>{idx>0?<hr />:<span />}{idx+1 + '. ' +
question.split('\n').map((item) => {return (item);})}</div>)
);
return (
<div style={{ width: '100%' }}>
{ qList }
</div>
);
问题数据:
这是第一行。\n第二个\n 这是第三个
预期结果:
这是第一行。
第二个
,这是第三个
我得到的是:
这是第一行。,第二个,这是第三个
参考
更新答案:
const qList = qs.map((question, idx) => (
<div key={idx}>
{idx>0?<hr />:<span />}
{<span>
{idx+1 + '. '}
</span>}
{
question.split('\n').map(item =>(
<span>
{item}
<br />
</span>
))
}
</div>
));
return (
<div style={{ width: '100%' }}>
{ qList }
</div>
);
解决方案
class Application extends React.Component {
render() {
let qlist = ["this is line first line.\nThe second\n and this is third"];
return (
<div style={{ width: '100%' }}>
{
qlist.map((qs, idx) => (
<div key={idx}>
{
qs.split("\n").map(item => (
<span>
{item}
<br/>
</span>
))
}
</div>
))
}
</div>
);
}
}
推荐阅读
- .net - 如何创建 dotNet 6 自安装服务?
- python - pingouin.rm_anova,重复测量方差分析错误。蟒蛇,熊猫
- python - 如何使用 AWS Glue 和其他 AWS 服务从 Oracle 数据库中提取数据
- visual-studio-code - 自动完成在 VSCode GraphQL 扩展中不起作用
- python - pandas 按列值分组并提取一列文本
- python - 当有相似的值时,如何在熊猫数据框中删除行?
- python - 星期字段中的气流哈希“#”运行不正常
- google-cloud-platform - Google AI Platform:错误代码 400:响应大小太大
- pandas - 如何从 DataFrame 中提取一行作为 Series,并将 DataFrame 中的列名作为 Series 中的行索引?
- java - 将数据库连接到 TableView 时出现“java.lang.reflect.InvocationTargetException”