【问题标题】:React js, Input type onclickReact js,输入类型onclick
【发布时间】:2020-06-01 11:13:53
【问题描述】:

大家好,我只是想知道

<input type="text" onClick={this.state}>
<h2> once i click the text box some of my div will show a "typing" then if not click the text the concept will be none *this is just a sample sorry guys if too messy my question</h2>

【问题讨论】:

    标签: reactjs input onclick


    【解决方案1】:

    如果我理解正确,您希望在单击 div 后显示“打字”一词。 我想这会对你有所帮助

    import React from "react";
    
    export default class Typing extends React.PureComponent {
        constructor(props) {
            super(props);
            this.state = {
                yourStateName: ""
            }
            this.onChangeState = this.onChangeState.bind(this);
        }
    
        onChangeState(e) {
            this.setState({
                yourStateName: "Typing"
            })
        }
    
        render() {
            const {yourStateName} = this.state;
            return (
                <div>
                    <input type="text" onClick={e => this.onChangeState(e)}>
                    value: {yourStateName}
                <div/>
            )
        }
    }
    

    【讨论】:

      【解决方案2】:

      尽管您的问题确实很混乱,但这里是您的示例代码。也许它会为你指明正确的方向。

      import React from "react";
      
      export default class Test extends React.PureComponent {
          constructor(props) {
              super(props);
              this.state = {
                  someState: ""
              }
              this.onChangeState = this.onChangeState.bind(this);
          }
      
          onChangeState(e) {
              this.setState({
                  someState: //logic here
              })
          }
      
          render() {
              const {someState} = this.state;
              return (
                  <>
                      <input type="text" onClick={e => this.onChangeState(e)}>
                      State value: {someState}
                  </>
              )
          }
      }
      

      当您点击选定的 div 时,您将调用 onChangeState 方法来操作您的状态。不要忘记每次状态更新都会导致重新渲染。 欲了解更多信息,请访问official React docs

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 2021-11-05
      相关资源
      最近更新 更多