简体   繁体   English

在我的 React 应用程序中,如何在此映射函数中的每个项目之后添加换行符?

[英]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.

相关问题 如何找出我的地图函数中的哪个项目应该受到我的函数调用的影响? - How can I single out which item in my map function should be affected by my function call in react? 如何在“&amp;”字符后添加换行符,而不是代码片段中显示的问题(添加额外的空格,而不是换行符)? - How can I add a newline after my “&” characters, instead of the issue shown in my code snippet (adding extra space, not line breaks)? React/MaterialUI - 如何在我的 object 中将每个 JSON 数组 map 放到它自己的单独表中? - React/MaterialUI - How can I map each JSON array in my object to it's own separate table? 如何在React中循环使用map函数? - How can I loop in a map function in React? map(item) function 在 react 中实现 redux 后无法将“item”作为道具传递给 JSX 组件 - map(item) function can't pass "item" to JSX component as props after implementing redux in react 如何为列表中的每个元素添加一个复选框,并能够在我的 React 应用程序中通过 state 控制它? - How can I add a checkbox to each element in the list and to be able to control it through state in my react app? 如何使用Reactjs向每个列表视图项添加按钮? - How can I add a button to each list view item with Reactjs? 我如何在React jsx渲染函数中的每个孩子之后添加元素? - How I can prepend an element after each child in a react jsx render function? 如何添加下拉菜单以将项目添加到我的ckeditor - How can I add a dropdown to add item to my ckeditor 无法将 Active class 添加到 React 中的组列表项 map function - Can't add Active class to group list item in react map function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM