简体   繁体   English

React:在其他状态的帮助下更新状态

[英]React: Update state with help of other state

I have three different state variables in a component.我在一个组件中有三个不同的状态变量。 Two of the states is connected to a range slider that updates the state when i move the slider.其中两个状态连接到一个范围滑块,当我移动滑块时它会更新状态。 One is for cost and one is for time.一种是为了成本,一种是为了时间。

My questions is, how do i update the third state based on information from the first two states?我的问题是,如何根据前两个状态的信息更新第三个状态?

function Calculator() {
  const [loanAmount, setLoanAmount] = useState(100000);
  const [loanTime, setLoanTime] = useState(5);

  const calculateCost = () => {
    const totalMonths = loanTime * 12;
    const monthlyFee = 0.00825;
    const monthlyFeePlusOne = 1.00825
    const totalPrice =
    (loanAmount*0.00825)*(Math.pow((1+0.00825), 60))/(Math.pow((1+0.00825), 60)-1);
    return Math.round(totalPrice);
  };
  const [calculation, setCalculation] = useState(calculateCost());


   <input
    className="slider"
    type="range"
    min="20000"
    max="200000"
    value={loanAmount}
    step="10000"
    onChange={(e) => setLoanAmount(e.target.value)}
  />
  <label>{loanAmount}</label>

  <input
    className="slider"
    type="range"
    min="2"
    max="10"
    value={loanTime}
    step="1"
    onChange={(e) => setLoanTime(e.target.value)}
    setCalculation
  />
  <label> {calculation} </label>

The third value isn't state because you can always calculate it from other state/props.第三个值不是状态,因为您始终可以从其他状态/道具计算它。

Try replacing尝试更换

const [calculation, setCalculation] = useState(calculateCost());

with

const calculation = calculateCost();

Making it state is redundant and complicates the code.使其状态是多余的并且使代码复杂化。

The first answer suggests doing it using useEffect which is the typical way a lot of developers do it, but that's not what useEffect is for, as stated in the React documentation: If your effect only adjusts some state based on another state, you might not need an effect.第一个答案建议使用useEffect进行操作,这是许多开发人员执行此操作的典型方式,但这不是useEffect的用途,如 React 文档中所述:如果您的效果仅根据另一个状态调整某些状态,您可能不会需要一个效果。

https://beta-reactjs-org-git-effects-fbopensource.vercel.app/learn/synchronizing-with-effects#you-might-not-need-an-effect https://beta-reactjs-org-git-effects-fbopensource.vercel.app/learn/synchronizing-with-effects#you-might-not-need-an-effect

So, you should perform this operation in the onChange event of your inputs since the state update is tied to it and not the render itself (which is what useEffect is for).因此,您应该在输入的onChange事件中执行此操作,因为状态更新与它相关联,而不是渲染本身(这是useEffect的用途)。

const handleLoanAmountInputChange = (e) => {
  const amount = e.currentTarget.value;
  setLoanAmount(amount);
  const cost = calculateCost(amount, loanTime); // you will ned to update your function to use parameters, this way it will also easier to test the function
  setCalculation(cost);
}

<input
  onChange={handleLoanAmountInputChange}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM