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

标签 button input reactjs onclick components

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

这是我的示例代码:

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/

相关文章:

css - 区分不同的 .CSS?

button - Plotly:如何为每个子图添加下拉菜单?

javascript - 样式<输入类型="file">

python - 字典条目被覆盖?

r - 将行添加到表中会删除 R Shiny 的所有以前的数据

Android - 单击按钮关闭 Activity

text - 自定义TextEdit,当它变宽时如何隐藏TextInput

javascript - react native -有办法将箭头导航图标更改为菜单图标吗?

javascript - 滚动到重新加载页面 react 的元素

javascript - React.js 和 ES6 : Any reason not to bind a function in the constructor