1

Sorry if the title is vague. My problem is that I have three button tags, and for each tag I want it to send a unique argument to selectSupplier(). However no matter what button I press selectSupplier() only receives the last value ("ultramar") as an argument.

selectSupplier(supplier){
    this.props.Obj.supplier = supplier
  }


  render() {
    //console.log("SUPPLIER", this.props)
    return (
      <div>
        <button onClick={this.showMenu}>
          Select Supplier
        </button>

        {
          this.state.showMenu
            ? (
              <div
                className="menu"
                ref={(element) => {
                  this.dropdownMenu = element;
                }}
              >
                <button value="Husky" onClick={this.selectSupplier("Husky")}> Husky </button>
                <button value="Shell" onClick={this.selectSupplier("Shell")}> Shell </button>
                <button value="Ultramar" onClick={this.selectSupplier("Ultramar")}> Ultramar</button>
              </div>
            )
            : (
              null
            )
        }
      </div>
    );
4
  • 5
    onClick={() => this.selectSupplier("Husky")}> You're calling the function instead of passing a reference to it. Commented Dec 11, 2019 at 21:28
  • 1
    Does this answer your question? React js onClick can't pass value to method Commented Dec 11, 2019 at 21:30
  • @EmileBergeron Thank you! I knew it was a simple fix, if you want to post an answer I'll gladly accept. Thank you for the link too, gave me some great info Ill follow Commented Dec 11, 2019 at 21:33
  • 1
    This was already answered multiple times on this site, so we'd rather close it as a duplicate to redirect anyone to the right place! Commented Dec 11, 2019 at 21:35

1 Answer 1

1

You can call like this :

this.selectSupplier.bind(this,"Husky");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.