如何在Reactjs中将多个功能添加到同一个useState中?

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

how to add multi functions to the same useState on Reactjs?

问题

我是React的初学者!现在正在学习。

如何设置多个函数来更改单个数据?我的意思是,我创建了下面的重置函数,它的目的是重置这些数字。它运行正常!

仅仅举个例子,我还想创建一个函数来将“hour”,“minute”和“second”更改为值“2”。

哪种方法最好?

它将有两个按钮,每个按钮将激活“resetTime”或“changeToTwoTime”。

// data()
const [hour, resetHour] = useState(4)
const [minute, resetMinute] = useState(8)
const [second, resetSecond] = useState(12)

// methods
const resetTime = () => {
    resetHour(0)
    resetMinute(0)
    resetSecond(0)
}
return (
    <div className="p-3">
        <MainButton 
            variantButton="outline-success"
            textButton="START"
            functionButton={resetTime}
        /> 
        <MainButton 
            variantButton="outline-success"
            textButton="START"
            functionButton={changeToTwoTime}
        />
    </div>
)

(请注意,上述代码示例中的changeToTwoTime函数还没有被定义,你需要定义它来更改“hour”,“minute”和“second”为值“2”)

英文:

I am a starter on React! Just learning right now

How can I set multi functions to change a single data? I mean, I created a reset function below which has the purpuse to reset the numbers. It is working fine!

Just for an example, I also want to create a function to change the 'hour', 'minute' and 'second' to the value '2'.

which is the best way to do it?

It will have two buttons and each button will activate the 'resetTime' or 'changeToTwoTime'.

//data()
const [hour, resetHour] = useState(4)
const [minute, resetMinute] = useState(8)
const [second, resetSecond] = useState(12)

// methods
const resetTime = () =&gt; {
    resetHour(0)
    resetMinute(0)
    resetSecond(0)
}

return (
        &lt;div className=&quot;p-3&quot;&gt;
             &lt;MainButton 
                  variantButton=&quot;outline-success&quot;
                  textButton=&quot;START&quot;
                  functionButton={resetTime}
              /&gt; 
              &lt;MainButton 
                  variantButton=&quot;outline-success&quot;
                  textButton=&quot;START&quot;
                  functionButton={changeToTwoTime}
              /&gt;
        &lt;/div&gt;
    )

答案1

得分: 2

只是举个例子,我也想创建一个函数来将'hour'、'minute'和'second'更改为值'2'。

实际上,您不需要复制函数。您只需简单地调整函数,使其接受一个参数,该参数将是日期的值。

const setTime = (value = 0) => {
  resetHour(value);
  resetMinute(value);
  resetSecond(value);
}

'value'参数是可选的。如果不带任何参数调用该函数,它将简单地将时间重置为0。如果传递'value',它将设置计时器为指定的'value'。

<div className="p-3">
  <MainButton 
    variantButton="outline-success"
    textButton="START"
    functionButton={() => setTime()} // 重置计时器
  /> 
  <MainButton 
    variantButton="outline-success"
    textButton="START"
    functionButton={() => setTime(2)} // 将其设置为2
  />
</div>
英文:

> Just for an example, I also want to create a function to change the 'hour', 'minute' and 'second' to the value '2'.

You don't need actually to duplicate the function. You could just simply adjust your function so it accepts an argument, which will be the value of the date.

const setTime = (value = 0) =&gt; {
  resetHour(value);
  resetMinute(value);
  resetSecond(value);
}

The value argument is optional. If the function is called without any arguments, it will simply reset the times to 0. If value is passed, it will set the timers to that specified value.

   &lt;div className=&quot;p-3&quot;&gt;
         &lt;MainButton 
              variantButton=&quot;outline-success&quot;
              textButton=&quot;START&quot;
              functionButton={() =&gt; setTime()} // reset timers
          /&gt; 
          &lt;MainButton 
              variantButton=&quot;outline-success&quot;
              textButton=&quot;START&quot;
              functionButton={() =&gt; setTime(2)} // set it to 2
          /&gt;
    &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月7日 03:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365625.html
匿名

发表评论

匿名网友

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

确定