[英]In my React application, how can I add a newline after each item in this map function?
I have this redux selector I've been working on in my React application.我一直在我的 React 应用程序中使用这个 redux 选择器。 I've made good progress but I've hit a wall with this last issue i'm working on and I feel like it shouldn't be that difficult to solve but I'm struggling.我已经取得了很好的进展,但我在处理的最后一个问题上遇到了困难,我觉得它不应该那么难解决,但我正在挣扎。
What I'm trying to achieve is after each item has been mapped, the next item must go to a new line.我想要实现的是在每个项目都被映射后,下一个项目必须转到一个新行。
export const vLineRejectionSelector = createSelector(
selectedVIdSelector,
linesSelector,
(id, lines) =>
lines
.filter(line => line.id === id)
.map(
(rejectString, index) =>
`Line: ${index + 1} ${rejectString.rejectReason}`
)
);
The only relevant code to look at in this is the map function.唯一需要查看的相关代码是 map 函数。 I want each item to go to a new line as its being mapped.我希望每个项目在被映射时转到一个新行。
The output should look something like:输出应该类似于:
Line 1: Reject Reason One
Line 2: Reject Reason Two
Instead the output looks like:相反,输出看起来像:
Line1: Reject ReasonOneLine2: Reject Reason Two
This is being rendered in JSX as well这也在 JSX 中呈现
The value of this is passed around as a prop and gets rendered in the JSX like: this 的值作为 prop 传递并在 JSX 中呈现,例如:
<Typography variant="body2">
{rejectReason}
{reasons && reasons.join(', ')}
</Typography>
Its value is {rejectReason}.它的值为 {rejectReason}。
I would advise against composing JSX in your selector and rather just return the lines as an array as you are doing currently, but then map it to either a list or a simple <br />
joined list in the render()
function.我建议不要在您的选择器中组合 JSX,而只是像您当前所做的那样将行作为数组返回,然后将其映射到列表或render()
函数中的简单<br />
连接列表。 This keeps your selector more easily testable and also doesn't mix state selection concerns with presentational concerns.这使您的选择器更易于测试,并且不会将状态选择问题与表现问题混合在一起。
Eg: in your container例如:在您的容器中
const mapStateToProps = (state) => {
return {
rejectReason: vLineRejectionSelector(state)
}
}
const SomeComponentContainer = connect(
mapStateToProps,
mapDispatchToProps
)(SomeComponent)
export default SomeComponentContainer
and then in your SomeComponent
s render function:然后在你的SomeComponent
的渲染函数中:
<Typography variant="body2">
{this.props.rejectReason.map((rejectReason) => <>Line: {index + 1} {rejectReason}<br /></>)}
</Typography>
Try to use <br />
tag at the end of each line.尝试在每行末尾使用<br />
标签。
Like Line: ${index + 1} ${rejectString.rejectReason}<br />
喜欢Line: ${index + 1} ${rejectString.rejectReason}<br />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.