如何在React中使用props使用map方法?

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

How to use the map method in react using props?

问题

  1. import History from "./History";
  2. import { useState } from "react";
  3. const Input = function () {
  4. const arr = [];
  5. const [expense, setExpense] = useState("");
  6. const [amount, setAmount] = useState("");
  7. const [state, setState] = useState(false);
  8. const expenseChangeHandler = function (e) {
  9. setExpense(e.target.value);
  10. };
  11. const amountChangeHandler = function (e) {
  12. setAmount(e.target.value);
  13. };
  14. const clickHandler = function () {
  15. setState(true);
  16. arr.push({
  17. expense: expense,
  18. amount: amount,
  19. });
  20. };
  21. return (
  22. <div>
  23. <input value={expense} onChange={expenseChangeHandler}></input>
  24. <input value={amount} onChange={amountChangeHandler}></input>
  25. <button onClick={clickHandler}>Add</button>
  26. {state && <History arr={arr}></History>}
  27. </div>
  28. );
  29. };
  30. export default Input;
  1. import "./History.css";
  2. const History = function (props) {
  3. const arr = props.arr;
  4. const newArr = arr.map((el) => (
  5. <p key={el.expense}>{el.expense}</p>
  6. )); // Added a key to each 'p' element for React's optimization
  7. return (
  8. <div className="div1">
  9. <h2>History</h2>
  10. <div>{newArr}</div>
  11. </div>
  12. );
  13. };
  14. export default History;

To render the expenses from the arr array in the History component, you can map through the arr and create p elements for each expense. I also added a key prop to each p element for React's optimization. This should display the expenses in the History component when the button in the Input component is clicked.

英文:
  1. import History from &quot;./History&quot;;
  2. import { useState } from &quot;react&quot;;
  3. const Input = function () {
  4. const arr = [];
  5. const [expense, setExpense] = useState(&quot;&quot;);
  6. const [amount, setAmount] = useState(&quot;&quot;);
  7. const [state, setState] = useState(false);
  8. const expenseChangeHandler = function (e) {
  9. setExpense(e.target.value);
  10. };
  11. const amountChangeHandler = function (e) {
  12. setAmount(e.target.value);
  13. };
  14. const clickHandler = function () {
  15. setState(true);
  16. arr.push({
  17. expense: expense,
  18. amount: amount,
  19. });
  20. };
  21. return (
  22. &lt;div&gt;
  23. &lt;input
  24. value={expense}
  25. onChange={expenseChangeHandler}
  26. &gt;&lt;/input&gt;
  27. &lt;input
  28. value={amount}
  29. onChange={amountChangeHandler}
  30. &gt;&lt;/input&gt;
  31. &lt;button onClick={clickHandler}&gt;Add&lt;/button&gt;
  32. {state &amp;&amp; &lt;History arr={arr}&gt;&lt;/History&gt;}
  33. &lt;/div&gt;
  34. );
  35. };
  36. export default Input;

Basically whats important to note here is that I am passing in the arr array as a prop for the History component.

  1. import &quot;./History.css&quot;;
  2. const History = function (props) {
  3. const arr = props.arr;
  4. const newArr = arr.map((el) =&gt; &lt;p&gt;{el.expense}&lt;/p&gt;);
  5. return (
  6. &lt;div className=&quot;div1&quot;&gt;
  7. &lt;h2&gt;History&lt;/h2&gt;
  8. &lt;div&gt;{newArr}&lt;/div&gt;
  9. &lt;/div&gt;
  10. );
  11. };
  12. export default History;

When the button in the parent component, Input, is clicked all it renders is 'History', from the h2 tag but I also want it to render the expense from the arr array. Any help?

答案1

得分: 1

问题出现在Input组件上,你需要使用useStatearr作为状态变量。

  1. const [arr, setArr] = useState([])

而不是使用arr.push,应该使用setArr(arr => [...arr, {expense, amount}])

英文:

The problem exists on Input component, you need to make arr as state variable using useState.

const [arr, setArr] = useState([])

And instead of arr.push, setArr(arr =&gt; [...arr, {expense, amount}])

答案2

得分: 0

我已解决了这个问题,这里是链接 https://codesandbox.io/s/peaceful-williamson-g8r2vl?file=/src/App.js

你需要将 "arr" 设置为状态变量,并更新它为 [...arr, {newarritem}]。

英文:

I have solved the issue, here is the link https://codesandbox.io/s/peaceful-williamson-g8r2vl?file=/src/App.js

you need a have arr as state variable and update it [...arr,{newarritem}]

huangapple
  • 本文由 发表于 2023年7月5日 01:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614679.html
匿名

发表评论

匿名网友

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

确定