[英]How to change button text in ReactJS
How to change the button text when someone click on?当有人单击时如何更改按钮文本?
Code:代码:
<Button disabled={this.state.disabled}
type="primary"
htmlType="submit"
style={{
background: '#ff9700',
fontWeight: 'bold',
border: 'none',
float: 'center',
}}
loading={this.state.loading}
onClick={this.enterLoading}
value="Next"
id="buttontext"
onClick="changeText()"
>
Next
</Button>
Mayank is correct.马扬克是对的。
Create a variable called "text" (or whatever you choose) and put that instead of "Next".创建一个名为“text”(或您选择的任何内容)的变量并将其放置在“Next”中。
state = {
text: "Next"
}
changeText = (text) => {
this.setState({ text });
}
render() {
const { text } = this.state //destucture state
return (
<Button
onClick={ () => { this.changeText("newtext")} }> {text} </Button> )...etc
Note: this method will always change the text to "newtext" when you click.注意:此方法将在您单击时始终将文本更改为“newtext”。 You can pass a variable there as well to make it more dynamic.
您也可以在那里传递一个变量以使其更具动态性。
Hope this helps.希望这可以帮助。
Update: Just saw Mayank comment.更新:刚看到 Mayank 评论。 That code is essentially what I have.
该代码基本上就是我所拥有的。 Just a tip you no longer need a constructor and you don't have to bind your methods anymore.
只是一个提示,您不再需要构造函数,也不必再绑定方法。
Same thing but with the useState
hook.同样的事情,但使用
useState
钩子。 Instead of calling the state variable text
, I am using buttonText
to be more explicit.我没有调用状态变量
text
,而是使用buttonText
来更加明确。 The updated version would look like:更新后的版本如下所示:
import { useState } from 'React';
const [buttonText, setButtonText] = useState("Next"); //same as creating your state variable where "Next" is the default value for buttonText and setButtonText is the setter function for your state variable instead of setState
const changeText = (text) => setButtonText(text);
return (
<Button onClick={() => changeText("newText")}>{buttonText}</Button>
)
You can omit the changeText
function all together and have this:您可以一起省略
changeText
函数并具有以下功能:
return (
<Button onClick={() => setButtonText("newText")}>{buttonText}</Button>
)
Adding an update to answer a question in the comments: "If I wanted to use a setTimout to bring the button back to the previous text after 1 second where would I add that in?"添加更新以回答评论中的问题:“如果我想使用 setTimout 将按钮在 1 秒后返回上一个文本,我将在哪里添加?”
There are two ways that comes to mind: add the setTimeout
to the changeText
function or create an effect that depends on the buttonText
.想到了两种方法:将
setTimeout
添加到changeText
函数或创建依赖于buttonText
的效果。
change text更改文本
You can just pop the setTimeout right in this function.你可以直接在这个函数中弹出 setTimeout 。
Goes from this从此
const changeText = (text) => setButtonText(text);
to this对此
const initialState = "Next";
const [buttonText, setButtonText] = useState(initialState); //same as creating your state variable where "Next" is the default value for buttonText and setButtonText is the setter function for your state variable instead of setState
const changeText = (text) => {
setButtonText(text);
setTimeout(() => setButtonText(initialState), [1000])
}
We add the initialState
variable as a const to keep track of the "previous text".我们将
initialState
变量添加为常量以跟踪“先前的文本”。 Since, it should never change we could define it in all caps snake case like const INITIAL_STATE
meh your choice.因为,它永远不会改变,我们可以在所有大写蛇形情况下定义它,例如
const INITIAL_STATE
meh 你的选择。
useEffect使用效果
We still need to define that initialState
variable, again so we can keep track of the original.我们仍然需要定义那个
initialState
变量,以便我们可以跟踪原始变量。 Then we can create a useEffect
which is a React hook that allows you to "hook" into changes of a variable (that's only a part of useEffect
, just enough to get us going here).然后我们可以创建一个
useEffect
,它是一个 React 钩子,它允许您“钩住”变量的更改(这只是useEffect
的一部分,足以让我们进入这里)。
We can break the effect down into two essential parts : the body or callback of the effect, what do we want to do when the effect runs and the dependency or what triggers the effect to run.我们可以将效果分解为两个基本部分:效果的主体或回调,效果运行时我们想要做什么以及依赖或触发效果运行的内容。 In this case, our callback will be
setTimeout
and set the button text inside that timeout and our buttonText
will trigger the effect.在这种情况下,我们的回调将是
setTimeout
并在该超时内设置按钮文本,我们的buttonText
将触发效果。
Here's the effect:效果如下:
useEffect(() => {
if(buttonText !== initialState){
setTimeout(() => setButtonText(initialState), [1000])
}
}, [buttonText])
Anytime the state variable buttonText
changes this effect will run. buttonText
状态变量buttonText
发生变化时,此效果就会运行。 We start at我们开始于
buttonText = initialState // "Next"
the effect runs and checks the if
.效果运行并检查
if
。 Since buttonText
equals the initialState the conditions evaluates to false
and we terminate the callback and the effect.由于
buttonText
等于 initialState,因此条件评估为false
,我们终止回调和效果。
When the user clicks the button, changeText
executes and sets the buttonText
state which updates the variable triggering the effect.当用户单击按钮时,
changeText
执行并设置buttonText
状态,该状态更新触发效果的变量。 Now we run that if
check again and this time it passes so we execute the setTimeout
.现在我们再次运行
if
check 并且这次它通过了所以我们执行setTimeout
。
Inside the timeout we are setting state so the effect runs again and this time it fails because we just changed the state back to initialState
.在超时内,我们正在设置状态,因此效果再次运行,这次它失败了,因为我们刚刚将状态更改回了
initialState
。
I recommend throwing a debugger in there or some logs to follow the trail
我建议在那里扔一个调试器或一些日志来跟踪
Long winded explanation.冗长的解释。 Here's what the whole component would look like using the effect approach.
这是使用效果方法的整个组件的外观。
import React, { useState, useEffect } from "react";
const FancyButton = () => {
const initialState = "Next";
const [buttonText, setButtonText] = useState("Next"); //same as creating your state variable where "Next" is the default value for buttonText and setButtonText is the setter function for your state variable instead of setState
// the effect
useEffect(() => {
if(buttonText !== initialState){
setTimeout(() => setButtonText(initialState), [1000])
}
}, [buttonText])
const changeText = (text) => setButtonText(text);
return (
<button type="button" onClick={() => changeText("newText")}>{buttonText}</button>
)
};
I added the
type
on the button because that's a good practice.我在按钮上添加了
type
,因为这是一个很好的做法。 And changed "Button" to "button".并将“按钮”更改为“按钮”。 You can certainly have any component there you want, this works better for copying and pasting
你当然可以有任何你想要的组件,这更适合复制和粘贴
Where you wrote "Next", the button text, do this instead:在你写“下一步”的地方,按钮文本,改为:
{this.state.disabled ? 'Disabled...' : 'Next'}
I this will display "Disabled..." when the state.disabled == true, and 'Next' when state.disabled == false.我这将在 state.disabled == true 时显示“Disabled...”,当 state.disabled == false 时显示“Next”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.