Non
Non

Reputation: 8589

How to handle checkbox in this case?

I am sending some data to the database through a post request, and I have a checkbox in my app that I need to handle with a state

I am using this library which is confusing me a bit.

Here I have my checkbox in the render method

<Checkbox value="Active" ref="Active" defaultChecked={true} onCheck={this._isChecked} />

and the function to handle it

  _isChecked = () => {
    console.log('Checked: ');
  }

I need it to be checked = true by default.

Now, here I have the function I am using in order to send the data to the post request

  _addDealer = () => {
    CreateDealersActions.createDealer({
      DealerName : this.refs.DealerName.getValue(),
      CardId     : this.refs.CardId.getValue(),
      NickName   : this.refs.NickName.getValue(),
      Picture    : 'http://lorempixel.com/150/150/',
      Active     : // HERE I NEED TO SET THE VALUE OF THE CHECKBOX
      LegalId    : this.refs.LegalId.getValue(),
      TypeId     : this.refs.TypeId.getValue(),
    });
  }

Look at the Active : // HERE I NEED TO SET THE VALUE OF THE CHECKBOX part in that function, there is where I need to put the value of the check box.

So, what should I know in order to send the proper data in the key Active, as I am using this library, do I need to put state? Or is there any other way?

Upvotes: 2

Views: 48

Answers (1)

Non
Non

Reputation: 8589

I am going to answer this question because probably some of you doesn't know the library I am using which is a pain in the ass and the docs are horrible.

You don't need to set an initial state because the checkbox component brings one attached already. So, in order for you to get the value of the checkbox, you don't need to set a function. With this I did is enough:

<Checkbox value="Active" ref="Active" defaultChecked={true} />

  _addDealer = () => {
    CreateDealersActions.createDealer({
      DealerName : this.refs.DealerName.getValue(),
      CardId     : this.refs.CardId.getValue(),
      NickName   : this.refs.NickName.getValue(),
      Picture    : 'http://lorempixel.com/150/150/',

      Active     : this.refs.Active.state.switched,

      LegalId    : this.refs.LegalId.getValue(),
      TypeId     : this.refs.TypeId.getValue(),
    });
  }

so: this.refs.Active.state.switched is enough to get the proper value of the checkbox.

Upvotes: 1

Related Questions