在REACT中如何垂直渲染数组?

huangapple go评论105阅读模式
英文:

How to render array vertically in REACT?

问题

Sure, here is the translated code part:

  1. 我正在创建一个问答应用我想要以以下方式显示结果
  2. > 问题:
  3. 你的答案:
  4. 正确答案:
  5. 但是目前我的代码显示成一行就像这样请查看图片链接以查看实际结果):
  6. > 问题: 你的答案: 正确答案:
  7. 图片链接: [https://ibb.co/30v89qS][1]
  8. 以下是我的代码
  9. var indents = [];
  10. for (var i = 0; i < questions.length; i++) {
  11. indents.push(<span><p>问题: {[questionWrongSelection[i], `你的答案: ${wrongAnswer[i]}`, `正确答案:\n ${rightAnswer[i]}`]}</p></span>);
  12. }
  13. 这是 div 的样式-
  14. <div className='wrongselection'>
  15. <hr />
  16. <h3>错误答案</h3>
  17. <p>
  18. <p>
  19. {indents.map(indent=><div>{indent}</div>)}
  20. </p>
  21. </p>
  22. </div>
  23. 我尝试使用 .map 方法但由于某种原因它对我不起作用或者我可能使用方法不对任何帮助将不胜感激
  24. [1]: https://i.stack.imgur.com/swv9q.png

Please note that I've translated the code part, as requested. If you have any further questions or need assistance with this code, feel free to ask.

英文:

I am creating a quiz app. I want to display result as follows

> Question:
Your Answer:
Correct answer:

But currently my code is displaying in a line like this (please see image link to view actual results):-

>Question: Your Answer: Correct Answer:
Image Link: https://ibb.co/30v89qS

below is my code

  1. var indents = [];
  2. for (var i = 0; i < questions.length; i++) {
  3. indents.push(<span><p>Question: {[questionWrongSelection[i], `Your Answer: ${wrongAnswer[i]}`, `Correct Answer:\n ${rightAnswer[i]}`]}</p></span>);
  4. }

And this is how the div looks:-

  1. <div className='wrongselection'>
  2. <hr />
  3. <h3>Wrong Answers</h3>
  4. <p>
  5. <p>
  6. {indents.map(indent=><div>{indent}</div>)}
  7. </p>
  8. </p>
  9. </div>

I tried .map method but for some reason it is not working for me or may be I am using it in a wrong way. Any help will be highly appreciated.

答案1

得分: 0

只需将每个单独的<p>标签中的内容括起来。

  1. var indents = [];
  2. for (var i = 0; i < questions.length; i++) {
  3. indents.push(
  4. <span>
  5. <p>问题:{questionWrongSelection[i]}</p>
  6. <p>你的答案:{wrongAnswer[i]} </p>
  7. <p>正确答案:{rightAnswer[i]}</p>
  8. </span>
  9. );
  10. }
英文:

Just enclose the contents on each separate <p> tags.

  1. var indents = [];
  2. for (var i = 0; i < questions.length; i++) {
  3. indents.push(
  4. <span>
  5. <p>Question: {questionWrongSelection[i]}</p>
  6. <p>Your Answer: {wrongAnswer[i]} </p>
  7. <p>Correct Answer: {rightAnswer[i]}</p>
  8. </span>
  9. );
  10. }

答案2

得分: 0

  1. var indents = [];
  2. for (var i = 0; i < questions.length; i++) {
  3. indents.push(
  4. <div className="result">
  5. <div>
  6. <strong>Question:</strong> {questionWrongSelection[i]}
  7. </div>
  8. <div>
  9. <strong>Your Answer:</strong> {wrongAnswer[i]}
  10. </div>
  11. <div>
  12. <strong>Correct Answer:</strong> {rightAnswer[i]}
  13. </div>
  14. </div>
  15. );
  16. }

每个问题、答案和正确答案都包含在类名为 "result" 的

中。然后,您可以应用以下 CSS 样式以实现列布局:

  1. .result {
  2. display: flex;
  3. flex-direction: column;
  4. margin-bottom: 10px;
  5. }
  6. .result div {
  7. margin-bottom: 5px;
  8. }

请确保在您的组件中包括这些 CSS 样式,或者将其放入一个单独的 CSS 文件中,然后在您的组件中进行导入。display: flex; 属性与 flex-direction: column; 创建了结果的列布局,而 margin-bottom 属性为每个结果之间添加了一些间距。

最后,在您的组件中渲染 indents 数组:

  1. return <div>{indents}</div>;

希望这对您有所帮助!

  1. <details>
  2. <summary>英文:</summary>

var indents = [];

for (var i = 0; i < questions.length; i++) {
indents.push(
<div className="result">
<div>
<strong>Question:</strong> {questionWrongSelection[i]}
</div>
<div>
<strong>Your Answer:</strong> {wrongAnswer[i]}
</div>
<div>
<strong>Correct Answer:</strong> {rightAnswer[i]}
</div>
</div>
);
}

  1. each question, answer, and the correct answer is wrapped inside a &lt;div&gt; with the class name &quot;result&quot;. You can then apply CSS styles to achieve the column layout
  2. css below -

.result {
display: flex;
flex-direction: column;
margin-bottom: 10px;
}

.result div {
margin-bottom: 5px;
}

  1. Make sure to include the CSS styles in your component or in a separate CSS file that is imported into your component. The display: flex; property with flex-direction: column; creates a column layout for the results, and the margin-bottom properties add some spacing between each result.
  2. Finally, render the indents array in your component:

return <div>{indents}</div>;

  1. Hope this will help!!
  2. </details>

huangapple
  • 本文由 发表于 2023年6月6日 12:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定