My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to use push method in reactjs

Md shahporan's photo
Md shahporan
·Mar 20, 2020

I want to push my data by using onChange method in reactjs . For example, I have got initial State

state = { checked: [], permission: {}, data: []}; I want to get this object :

permission : {group:["value2","value1"],topGroup:["value2","value1"]} but the problem is that when I click onChange , It will give me one object each time such as

permission : {group:["value2","value1"]}.

If I add second checkbox , it should add data inside permission object, but I can not push my second data in permission objects . permission objects return one property each time . How to resolve this problem ?

 onChange = value => checked => {
    this.setState({ checked }, () => {
      this.setState(prevState => {
        Object.assign(prevState.permission, { [value]: this.state.checked });
      });
    });
  };

  <CheckboxGroup
            options={options}
            value={checked}
            onChange={this.onChange(this.props.label)}
          />

Here is my codesanbox:codesandbox.io/s/stackoverflow-a-60764570-3..