0

I have a simple code like this:

class App extends Component {
    connect = async (parameter) => {
      //some code here
    }
render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            <label>Block Number:</label>
            <input type="number" id="input"/>
            <button type="submit" onClick={() => this.connect()}>Button</button>
          </p>
        </header>
      </div>
    )
  }
}

How can I pass input value from input to connect function's parameter?

2

4 Answers 4

1

There are various ways. I would like to do it using state.

class App extends Component {
  state = {
    number: ''
  }

  connect = async () => {
    //some code here
    alert(this.state.number)
  }
render() {
  return (
    <div className="App">
      <header className="App-header">
        <p>
          <label>Block Number:</label>
          <input type="number" id="input" value={this.state.number} onChange={ e => this.setState({number: e.target.value})}/>
          <button type="submit" onClick={() => this.connect()}>Button</button>
        </p>
      </header>
    </div>
  )
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

Hope its work for you

class App extends Component {
    connect = async (event) => {
      event.preventDefault();
      this.textInput.value; // Your Value
    }
render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            <label>Block Number:</label>
            <input type="number" ref={(input) => this.textInput = input}  id="input"/>
            <button type="submit" onClick={() => this.connect()}>Button</button>
          </p>
        </header>
      </div>
)

} }

Comments

0

You should consider making your input a controlled input, rather than an uncontrolled one.

Anyway, here is how to do that based on your existing solution:

<button type="submit" onClick={() => this.connect(document.getElementById("input").value)}>Button</button>

That being said, since you are on the same DOM tree, you could access it directly from connect without passing it as a parameter.

connect = async (parameter) => {
  document.getElementById("input").value;
  //some code here
}

3 Comments

Why do you recommend using document.getElement by id here?
@wiesson Because the input component is uncontrolled. You need to access the DOM directly. You could use refs, sure, but it's just an escape hatch which also accesses the DOM directly.
what if there are multiple inputs ?
0

Thank you all for awnsers my question. I found this

    class App extends Component {
        constructor(props) {    
        super(props)
        this.state = {
            valueInput:""
        }
    }
        connect = async () => {
          let {valueInput} = this.state
          //some code here
        }
       _onchange=evt=>{
            this.setState({valueInput:evt.target.value})
  }
    render() {
        return (
          <div className="App">
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <p>
                <label>Block Number:</label><br></br>
            <input type="number" id="input"  onChange={this._onchange}/><br></br>
            <button type="submit" onClick={ this.connect}>Button</button>
              </p>
            </header>
          </div>
        )
      }
    }

its work for me perfectly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.