如何设置/更新从用户输入传递给React组件的prop。

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

How could I set/update a prop passed down to a React component from user input

问题

I have values in an array of objects that I wish to display as scoreBoard showing scores of two players after a number of sets in a game. I wish to have them in separate rows for the players. Thank you in anticipation.

The picture below shows scores after two sets.

How can I print the scores of each player in a separate row on the DOM?

Eg.

"player1Scores": 4 8 12

"player2Scores": 5 18 22

  1. import React from "react"
  2. import ReactDOM from "react-dom"
  3. export default function App(){
  4. const [players, setPlayers] = React.useState([{name: "Obi",
  5. age: 44, achievements: [12, 4, 16, 28]}, {name: "Chiadi",
  6. age: 47, achievements: [8, 10, 12, 21]}]);
  7. const scores = players.map((player) => (player.achievements.map( (score, ind) => (<h2 key={ind}>{score}</h2>)
  8. ))
  9. return ( {scores} )
  10. }
  11. ReactDOM.render(<App />, document.getElementById("root"))
英文:

I have values in an array of objects that I wish to display as scoreBoard showing scores of two players after a number of sets in a game. I wish to have them in separate rows for the players. Thank you in anticipation.

The picture below shows scores after two sets.
How can I print the scores of each player in a separate row on the DOM?
Eg.

"player1Scores": 4 8 12

"player2Scores": 5 18 22

  1. import React from &quot;react&quot;
  2. import ReactDOM from &quot;react-dom&quot;
  3. export default funtion App(){
  4. const [players, setPlayers] = React.useState([{name: &quot;Obi&quot;,
  5. age: 44, achievements: [12, 4, 16, 28]}, {name: &quot;Chiadi&quot;,
  6. age: 47, achievements: [8, 10, 12, 21]}]);
  7. const scores = players.map((player) =&gt; (player.achievements.map( (score, ind) =&gt; (&lt;h2 key={ind}&gt;{score}&lt;/h2&gt;)
  8. ))
  9. return ( {scores} )
  10. }
  11. ReactDOM.render(&lt;App /&gt;, document.getElementById(&quot;root&quot;))

答案1

得分: 0

我通过将数组 player.achievements 传递给一个组件并进行映射来解决了这个问题。

  1. const [players, setPlayers] = React.useState([{name: "Obi",
  2. age: 44, achievements: [12, 4, 16, 28]}, {name: "Chiadi",
  3. age: 47, achievements: [8, 10, 12, 21]}]);
  4. const scores = players.map((player) => (
  5. <ScoreComponent scores={player.achievements}/>
  6. ))

然后我能够在子组件 (ScoreComponent) 中映射 props.scores 并显示成就或分数。

英文:

I solved the problem by passing the array, player.achievements to a component and mapped through it.

  1. const [players, setPlayers] = React.useState([{name: &quot;Obi&quot;,
  2. age: 44, achievements: [12, 4, 16, 28]}, {name: &quot;Chiadi&quot;,
  3. age: 47, achievements: [8, 10, 12, 21]}]);
  4. const scores = players.map((player) =&gt; (
  5. &lt;ScoreComponent scores ={player.achievements}/&gt;)
  6. )

I was then able to map through props.scores in the child component (ScoreComponent) and have the achievements or scores displayed."

huangapple
  • 本文由 发表于 2023年4月11日 07:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981361.html
匿名

发表评论

匿名网友

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

确定