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

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

How to use the map method in react using props?

问题

import History from "./History";
import { useState } from "react";

const Input = function () {
  const arr = [];
  const [expense, setExpense] = useState("");
  const [amount, setAmount] = useState("");
  const [state, setState] = useState(false);

  const expenseChangeHandler = function (e) {
    setExpense(e.target.value);
  };
  const amountChangeHandler = function (e) {
    setAmount(e.target.value);
  };
  const clickHandler = function () {
    setState(true);
    arr.push({
      expense: expense,
      amount: amount,
    });
  };
  return (
    <div>
      <input value={expense} onChange={expenseChangeHandler}></input>
      <input value={amount} onChange={amountChangeHandler}></input>
      <button onClick={clickHandler}>Add</button>
      {state && <History arr={arr}></History>}
    </div>
  );
};

export default Input;
import "./History.css";

const History = function (props) {
  const arr = props.arr;
  const newArr = arr.map((el) => (
    <p key={el.expense}>{el.expense}</p>
  )); // Added a key to each 'p' element for React's optimization
  return (
    <div className="div1">
      <h2>History</h2>
      <div>{newArr}</div>
    </div>
  );
};

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.

英文:
 import History from &quot;./History&quot;;
import { useState } from &quot;react&quot;;

const Input = function () {
  const arr = [];
  const [expense, setExpense] = useState(&quot;&quot;);
  const [amount, setAmount] = useState(&quot;&quot;);
  const [state, setState] = useState(false);

  const expenseChangeHandler = function (e) {
    setExpense(e.target.value);
  };
  const amountChangeHandler = function (e) {
    setAmount(e.target.value);
  };
  const clickHandler = function () {
    setState(true);
    arr.push({
      expense: expense,
      amount: amount,
    });
  };
  return (
    &lt;div&gt;
      &lt;input
        value={expense}
        onChange={expenseChangeHandler}
      &gt;&lt;/input&gt;
      &lt;input
         value={amount}
        onChange={amountChangeHandler}
      &gt;&lt;/input&gt;
      &lt;button onClick={clickHandler}&gt;Add&lt;/button&gt;
      {state &amp;&amp; &lt;History arr={arr}&gt;&lt;/History&gt;}
    &lt;/div&gt;
  );
};

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.

 import &quot;./History.css&quot;;

const History = function (props) {
  const arr = props.arr;
  const newArr = arr.map((el) =&gt; &lt;p&gt;{el.expense}&lt;/p&gt;);
  return (
    &lt;div className=&quot;div1&quot;&gt;
      &lt;h2&gt;History&lt;/h2&gt;
      &lt;div&gt;{newArr}&lt;/div&gt;
    &lt;/div&gt;
  );
};

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作为状态变量。

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:

确定