gpt4 book ai didi

button - 单击按钮即可停用输入 react

转载 作者:行者123 更新时间:2023-12-03 13:02:27 24 4
gpt4 key购买 nike

我有这个基本组件,我希望每当单击按钮时停用或激活文本字段。我怎样才能实现这个目标?

这是我的示例代码:

import React from "react";
import Button from 'react-button'

const Typing = (props) => {
var disabled = "disabled";
var enabled = !disabled;

const handleUserInput = (event) => props.onUserInput(event.target.value);
const handleGameClik = (props) => {

disabled = enabled;
}
return(
<div>

<input
className = "typing-container"
value = {props.currentInput}
onChange = {handleUserInput}
placeholder=" ^__^ "
disabled = {disabled}/>
<Button onClick = {handleGameClik}> Start Game </Button>
<Button> Fetch Data </Button>

</div>
);
};

最佳答案

使用状态的简化解决方案可能如下所示:

class Typing extends React.Component {
constructor(props) {
super(props);
this.state = { disabled: false }
}
handleGameClik() {
this.setState( {disabled: !this.state.disabled} )
}
render() {
return(
<div>
<input
className = "typing-container"
placeholder= " type here "
disabled = {(this.state.disabled)? "disabled" : ""}/>
<button onClick = {this.handleGameClik.bind(this)}> Start Game </button>
<button> Fetch Data </button>
</div>
);
}
};

工作中Codepen here .

关于button - 单击按钮即可停用输入 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36773671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com