0

I want to store button values when a user clicks on them in react and store these values in an array for later use. However, given my basic knowledge in REACT I am getting errors and unable to do so.

I am using console.log on the preference array. But I am not getting the values stored on the preference array.

    class Dashboard extends Component {
      constructor(props) {
        super(props);
        this.state = {
          getPreferences: false,
          preferenceArray: []
        };

        // This binding is necessary to make `this` work in the callback
        this.handleClick = this.handleClick.bind(this);
      }
    handleClick() {
    this.setState(state => ({
      getPreferences: !state.getPreferences,
      preferenceArray: state.preferenceArray.push(this.handleClick.value)
    }));


  render() {
    const preference = this.state.preferenceArray;
    console.log(preference[0]);

    const { user } = this.props.auth;
    const { profile, loading } = this.props.profile;

    let dashboardContent;

    if (profile == null || loading) {
      dashboardContent = <Spinner />;
    } else {
      dashboardContent = (
        <h2>
          Select all issues that you are interested in:
          <hr />
          <br />
        </h2>
      );
    }

    return (
      <div className="dashboard">
        <div className="container">
          <div className="row">
            <div className="col-md-12">
              <h1 className="display-4">Dashboard {dashboardContent}</h1>
              <button
                type="button"
                name="Abortion"
                value={this.state.name}
                className="btn btn-secondary"
                onClick={this.handleClick}
              >
                Abortion
              </button>
              &nbsp;&nbsp;&nbsp;
              <button
                type="button"
                name="GayMarriage"
                value="Gay"
                className="btn btn-secondary"
                onClick={this.handleClick}
              >
                Gay Marriage
                {"     "}
              </button>
              &nbsp;&nbsp;&nbsp;
              <button
                type="button"
                name="Guns"
                value="Guns"
                className="btn btn-secondary"
 onClick={this.handleClick}


             >
                Guns
                {"     "}
              </button>
              &nbsp;&nbsp;&nbsp;
              <button
                type="button"
                name="HIR"
                value={this.state.name}
                className="btn btn-secondary"
              >
                Higher Interest Rates
                {"     "}


                onClick={this.handleClick}
              </button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

1 Answer 1

1

Well, from what I know all you have to do is basicly use the "event" to get the value.

for example.

handleClick(e) {
//e- the event
this.setState(state => ({
  getPreferences: !state.getPreferences,
  preferenceArray: state.preferenceArray.push(e.target.value)// the event traget value
}));
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks, but I am getting error of cannot read property value of null. Thanks
@ Talg123 any suggestions to get rid of this error?
hmm not really, Im not React "user" I work with Angular. but from what I know that should work. donno exectly why not. try to console.log the "e" and then the "e.target" before the this.setState... see what you get

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.