javascript - 在 React 中渲染状态元素
问题描述
我有一个名为的状态changedFields
,它是一个包含编辑数据时更改的字段的列表。
<Dialog
open={this.state.isDialogOpen}
onClose={this.handleClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle
id="responsive-dialog-title"
>
{'ARE YOU SURE YOU WISH TO MAKE THESE CHANGES?'}
</DialogTitle>
<DialogContent>
<DialogContentText>
<td style={{ 'font-weight': 'bold', 'border-bottom': '0px', 'margin-right': '2em' }}>You changed:</td>
<td>
<tr>{this.state.changedFields}</tr>
</td>
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={this.handleClose} color="primary">
DISCARD CHANGES
</Button>
<Button onClick={this.handleClose} color="primary" autoFocus>
YES, KEEP CHANGES
</Button>
</DialogActions>
</Dialog>
我是这样写对话的。这看起来像:
我试图将每个元素description
、trelloURL
和slackChannel
放在不同的行中,但这只是将每个元素放在一行中而没有空格。
如果我想将它们放在不同的行中,我该如何渲染它?
解决方案
目前,你只是这样做:
<tr>{this.state.changedFields}</tr>
...这将使用 React 的默认“将此数组转换为字符串”,然后一个接一个地输出元素。
class Example extends React.Component {
constructor(...args) {
super(...args);
this.state = {
changedFields: [
"description",
"trelloUrl",
"slackChannel"
]
};
}
render() {
return (
<table>
<tbody>
<tr>{this.state.changedFields}</tr>
</tbody>
</table>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
你需要做一些事情来创建你想要的换行符;也许是单独的行:
一种选择是将它们各自放在自己的行中:
{this.state.changedFields.map(field => <tr><td>{field}</td></tr>)}
class Example extends React.Component {
constructor(...args) {
super(...args);
this.state = {
changedFields: [
"description",
"trelloUrl",
"slackChannel"
]
};
}
render() {
return (
<table>
<tbody>
{this.state.changedFields.map(field => <tr><td>{field}</td></tr>)}
</tbody>
</table>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
td
或者如果合适,使用多个s将其放在一个中div
,因为它们默认为display: block
(或任何其他元素并适当地设置其样式):
<tr><td>{this.state.changedFields.map(field => <div>{field}</div>)}</td></tr>
class Example extends React.Component {
constructor(...args) {
super(...args);
this.state = {
changedFields: [
"description",
"trelloUrl",
"slackChannel"
]
};
}
render() {
return (
<table>
<tbody>
<tr>
<td>{this.state.changedFields.map(field => <div>{field}</div>)}</td>
</tr>
</tbody>
</table>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
您现在已经说过您希望它们在 旁边You changed
,而不是在不同的行中。这更简单:只需将它们放在同一个中td
:
<td style={{ 'font-weight': 'bold', 'border-bottom': '0px', 'padding-right': '2em' }}>You changed:</td>
<td>{this.state.changedFields.join(" ")}</td>
class Example extends React.Component {
constructor(...args) {
super(...args);
this.state = {
changedFields: [
"description",
"trelloUrl",
"slackChannel"
]
};
}
render() {
return (
<table>
<tbody>
<tr>
<td style={{ 'font-weight': 'bold', 'border-bottom': '0px', 'padding-right': '2em' }}>You changed:</td>
<td>{this.state.changedFields.join(" ")}</td>
</tr>
</tbody>
</table>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
旁注:强烈建议使用类或类似的 CSS,而不是内联样式。
推荐阅读
- elixir - Elixir Agents 可见性范围
- angular - Angular Reactive Forms,formName.reset() 有效但抛出错误'control.value is null'并且所有字段都标记为'红色'并且无效?
- r - 如何检查列表是否包含R中的某个元素
- asp.net-core - MSBuild 错误构建 ASP.Net Core 1.0 项目 - 未找到导入的项目
- excel - 从 Excel 复制到 Word 文档的剪贴板错误
- python - 如何在python中获取字符串中特定单词旁边的单词列表
- c++ - _get_second() 中的 xutility 堆栈溢出
- trace - 如何使用 sleuth/brave 传播具有不同标头名称的标头值
- node.js - MulterError:意外的字段,reactjs,vuejs,nodejs
- java - 从数组中返回 K 个最大的元素。(JAVA)