typescript 2个输入框,如何根据第一个值更新第二个值,反之亦然?

8qgya5xd  于 2023-02-13  发布在  TypeScript
关注(0)|答案(1)|浏览(127)

我的jsx文件中有以下代码

const [DataOne, setDataOne] = useState(null);
const [DataTwo, setDataTwo] = useState(null);
const [focused, setFocused] = useState(false);

function getValueOne(val) {
    setDataOne(val.target.value)
    console.log(val.target.value);
}
function getValueTwo(val) {
    setDataTwo(val.target.value)
    console.log(val.target.value);
}
const onFocus = () => setFocused(true);

return (
    <div>
        <div>
            <select>
                <option value={0}>Currency to send:</option>
                <option value={1}>Dollar</option>
                <option value={2}>Euro</option>
                <option value={3}>Pound</option>
            </select>
        </div>
        <div>
            <select>
                <option value={0}>Currency to receive:</option>
                <option value={1}>Dollar</option>
                <option value={2}>Euro</option>
                <option value={3}>Pound</option>
            </select>
        </div>
        <div>
            <input 
                type={'number'} 
                placeholder={'Fruit in'} 
                onChange={getValueOne} 
                value={DataTwo === null ? 0 :  (focused ? DataTwo / 2 : DataOne)}
                onFocus={onFocus}
            ></input>
        </div>
        <div>
            <input 
                type={'number'} 
                placeholder={'Fruit out'}
                onChange={getValueTwo} 
                value={DataOne === null ? 0 : (focused ? DataOne / 2 : DataTwo)}
            ></input>
        </div>
    </div>
)

我需要在每次更改输入字段(两个字段)后向api发送请求,如下所示-www.example.comexchangerate-api.com?from=USD?to=EUR?val=10094.67)其中val是一个输入框的值,response应该填充第二个输入域。(这是开始实现api fetch之前的中间步骤),比如如果我输入1,2应该出现在第二个框中;如果我在第二个框中输入2,1应该出现在第一个框中。 94.67) where val is a value of one of input boxes, response should fill the second input field. Now I just try to update the remaining input box value (This is an intermediate step before I'll start to implement api fetch), like if I type 1, 2 should appear in the second box. If I type 2 in the second box, 1 should appear in the first.
到目前为止,我可以看到价值观在变化,但事情并没有按计划进行,甚至没有接近。非常不可预测。
无论何时更改数字,如何更新相反的输入值?
你可以在这里看到复杂的逻辑
值= {数据一===空?0:(重点?DataOne/2:DataTwo)}但它不起作用,我只是尝试使以前的版本起作用,这里是value = {DataOne === null?:DataTwo)}明显的值= {DataOne}也不起作用。我肯定每次都将相反的输入框的DataOne切换为DataTwo。

qxsslcnc

qxsslcnc1#

我会用一点不同的方法来处理这个问题,正如你提到的,逻辑有点复杂,为什么要用值来计算呢,当你可以用onChange函数来计算的时候?

import { useState } from 'react';
import './App.css';

function App() {
    const [DataOne, setDataOne] = useState(null);
    const [DataTwo, setDataTwo] = useState(null);
    const [focused, setFocused] = useState(false);
    
    function getValueOne(val) {
        setDataOne(val.target.value);
        setDataTwo(val.target.value * 2);// If you put 1 in, put 2 for value 2
        console.log(val.target.value);
    }
    function getValueTwo(val) {
        setDataTwo(val.target.value);
        setDataOne(val.target.value / 2);// If you put 2 in, put 1 for value 1
    }
    const onFocus = () => setFocused(true);
    
    // Now your DataOne and DataTwo values are calculated, so just plug them in their respective spots
    return (
        <div>
            <div>
                <select>
                    <option value={0}>Currency to send:</option>
                    <option value={1}>Dollar</option>
                    <option value={2}>Euro</option>
                    <option value={3}>Pound</option>
                </select>
            </div>
            <div>
                <select>
                    <option value={0}>Currency to receive:</option>
                    <option value={1}>Dollar</option>
                    <option value={2}>Euro</option>
                    <option value={3}>Pound</option>
                </select>
            </div>
            <div>
                <input 
                    type={'number'} 
                    placeholder={'Fruit in'} 
                    onChange={getValueOne}
                    value={DataOne}
                    onFocus={onFocus}
                ></input>
            </div>
            <div>
                <input 
                    type={'number'} 
                    placeholder={'Fruit out'}
                    onChange={getValueTwo}
                    value={DataTwo}
                ></input>
            </div>
        </div>
    )
}

export default App;

这样,只要输入发生变化,您就可以执行所需的计算

相关问题