'onChange' event is getting called only once in react












0















I am using material-ui



Here my react component goes:



import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import React from "react";
import { withStyles } from "@material-ui/core/styles";

const styles = (theme) => ({
});

class SomeComponent extends React.Component {
static propTypes = {

};

state = {
checked: true,
}

handleChange = name => event => {
event.persist()
this.setState({
[name]: event.target.checked
}, () => {
if (event.target.checked) {
this.props.parentMethod1(event.target.value)
} else {
this.props.parentMethod2(event.target.value)
}
});
};

render() {
const { user } = this.props;
return (
<div>
<FormControlLabel
control={
<Checkbox
checked={this.state.checked}
onChange={this.handleChange('checked')}
value={user.id}
color="primary"
/>
}
label={user.first_name}
/>
</div>
);
}

}

export default withStyles(styles)(SomeComponent);


The problem is, I can select/unselect the checkBox only once.
After selecting/deselecting the checkbox, onChange event is not occuring.



Can you help me how to make Checkbox work as the way it is?
Thanks.



Here is the reproduced error:



https://codesandbox.io/s/y041zknrqv










share|improve this question




















  • 1





    Instead of using event.target.checked and event.target.value in the callback, you can put checked and value in variables the first thing you do in the function returned from handleChange: const { checked, value } = event.target and use that instead. This way you don't need to persists the event. Does that fix the issue?

    – Tholle
    Nov 15 '18 at 15:52













  • This will remove the warning but issue is not fixed yet.

    – Akhila Hegde
    Nov 15 '18 at 16:00











  • Hi there. I can't seem to reproduce the problem using similar code to yours: codesandbox.io/s/m788313rvp

    – AndyJ
    Nov 15 '18 at 17:01











  • Using just this much, we can't reproduce the error. The parent component has few synthetic events and it's too huge to add here. I will try to minimize the code and edit here.

    – Akhila Hegde
    Nov 15 '18 at 17:18











  • @AndyJ Please see the codesandbox.io/s/y041zknrqv

    – Akhila Hegde
    Nov 15 '18 at 18:49
















0















I am using material-ui



Here my react component goes:



import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import React from "react";
import { withStyles } from "@material-ui/core/styles";

const styles = (theme) => ({
});

class SomeComponent extends React.Component {
static propTypes = {

};

state = {
checked: true,
}

handleChange = name => event => {
event.persist()
this.setState({
[name]: event.target.checked
}, () => {
if (event.target.checked) {
this.props.parentMethod1(event.target.value)
} else {
this.props.parentMethod2(event.target.value)
}
});
};

render() {
const { user } = this.props;
return (
<div>
<FormControlLabel
control={
<Checkbox
checked={this.state.checked}
onChange={this.handleChange('checked')}
value={user.id}
color="primary"
/>
}
label={user.first_name}
/>
</div>
);
}

}

export default withStyles(styles)(SomeComponent);


The problem is, I can select/unselect the checkBox only once.
After selecting/deselecting the checkbox, onChange event is not occuring.



Can you help me how to make Checkbox work as the way it is?
Thanks.



Here is the reproduced error:



https://codesandbox.io/s/y041zknrqv










share|improve this question




















  • 1





    Instead of using event.target.checked and event.target.value in the callback, you can put checked and value in variables the first thing you do in the function returned from handleChange: const { checked, value } = event.target and use that instead. This way you don't need to persists the event. Does that fix the issue?

    – Tholle
    Nov 15 '18 at 15:52













  • This will remove the warning but issue is not fixed yet.

    – Akhila Hegde
    Nov 15 '18 at 16:00











  • Hi there. I can't seem to reproduce the problem using similar code to yours: codesandbox.io/s/m788313rvp

    – AndyJ
    Nov 15 '18 at 17:01











  • Using just this much, we can't reproduce the error. The parent component has few synthetic events and it's too huge to add here. I will try to minimize the code and edit here.

    – Akhila Hegde
    Nov 15 '18 at 17:18











  • @AndyJ Please see the codesandbox.io/s/y041zknrqv

    – Akhila Hegde
    Nov 15 '18 at 18:49














0












0








0


0






I am using material-ui



Here my react component goes:



import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import React from "react";
import { withStyles } from "@material-ui/core/styles";

const styles = (theme) => ({
});

class SomeComponent extends React.Component {
static propTypes = {

};

state = {
checked: true,
}

handleChange = name => event => {
event.persist()
this.setState({
[name]: event.target.checked
}, () => {
if (event.target.checked) {
this.props.parentMethod1(event.target.value)
} else {
this.props.parentMethod2(event.target.value)
}
});
};

render() {
const { user } = this.props;
return (
<div>
<FormControlLabel
control={
<Checkbox
checked={this.state.checked}
onChange={this.handleChange('checked')}
value={user.id}
color="primary"
/>
}
label={user.first_name}
/>
</div>
);
}

}

export default withStyles(styles)(SomeComponent);


The problem is, I can select/unselect the checkBox only once.
After selecting/deselecting the checkbox, onChange event is not occuring.



Can you help me how to make Checkbox work as the way it is?
Thanks.



Here is the reproduced error:



https://codesandbox.io/s/y041zknrqv










share|improve this question
















I am using material-ui



Here my react component goes:



import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import React from "react";
import { withStyles } from "@material-ui/core/styles";

const styles = (theme) => ({
});

class SomeComponent extends React.Component {
static propTypes = {

};

state = {
checked: true,
}

handleChange = name => event => {
event.persist()
this.setState({
[name]: event.target.checked
}, () => {
if (event.target.checked) {
this.props.parentMethod1(event.target.value)
} else {
this.props.parentMethod2(event.target.value)
}
});
};

render() {
const { user } = this.props;
return (
<div>
<FormControlLabel
control={
<Checkbox
checked={this.state.checked}
onChange={this.handleChange('checked')}
value={user.id}
color="primary"
/>
}
label={user.first_name}
/>
</div>
);
}

}

export default withStyles(styles)(SomeComponent);


The problem is, I can select/unselect the checkBox only once.
After selecting/deselecting the checkbox, onChange event is not occuring.



Can you help me how to make Checkbox work as the way it is?
Thanks.



Here is the reproduced error:



https://codesandbox.io/s/y041zknrqv







javascript reactjs material-ui dom-events






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:54







Akhila Hegde

















asked Nov 15 '18 at 15:48









Akhila HegdeAkhila Hegde

8318




8318








  • 1





    Instead of using event.target.checked and event.target.value in the callback, you can put checked and value in variables the first thing you do in the function returned from handleChange: const { checked, value } = event.target and use that instead. This way you don't need to persists the event. Does that fix the issue?

    – Tholle
    Nov 15 '18 at 15:52













  • This will remove the warning but issue is not fixed yet.

    – Akhila Hegde
    Nov 15 '18 at 16:00











  • Hi there. I can't seem to reproduce the problem using similar code to yours: codesandbox.io/s/m788313rvp

    – AndyJ
    Nov 15 '18 at 17:01











  • Using just this much, we can't reproduce the error. The parent component has few synthetic events and it's too huge to add here. I will try to minimize the code and edit here.

    – Akhila Hegde
    Nov 15 '18 at 17:18











  • @AndyJ Please see the codesandbox.io/s/y041zknrqv

    – Akhila Hegde
    Nov 15 '18 at 18:49














  • 1





    Instead of using event.target.checked and event.target.value in the callback, you can put checked and value in variables the first thing you do in the function returned from handleChange: const { checked, value } = event.target and use that instead. This way you don't need to persists the event. Does that fix the issue?

    – Tholle
    Nov 15 '18 at 15:52













  • This will remove the warning but issue is not fixed yet.

    – Akhila Hegde
    Nov 15 '18 at 16:00











  • Hi there. I can't seem to reproduce the problem using similar code to yours: codesandbox.io/s/m788313rvp

    – AndyJ
    Nov 15 '18 at 17:01











  • Using just this much, we can't reproduce the error. The parent component has few synthetic events and it's too huge to add here. I will try to minimize the code and edit here.

    – Akhila Hegde
    Nov 15 '18 at 17:18











  • @AndyJ Please see the codesandbox.io/s/y041zknrqv

    – Akhila Hegde
    Nov 15 '18 at 18:49








1




1





Instead of using event.target.checked and event.target.value in the callback, you can put checked and value in variables the first thing you do in the function returned from handleChange: const { checked, value } = event.target and use that instead. This way you don't need to persists the event. Does that fix the issue?

– Tholle
Nov 15 '18 at 15:52







Instead of using event.target.checked and event.target.value in the callback, you can put checked and value in variables the first thing you do in the function returned from handleChange: const { checked, value } = event.target and use that instead. This way you don't need to persists the event. Does that fix the issue?

– Tholle
Nov 15 '18 at 15:52















This will remove the warning but issue is not fixed yet.

– Akhila Hegde
Nov 15 '18 at 16:00





This will remove the warning but issue is not fixed yet.

– Akhila Hegde
Nov 15 '18 at 16:00













Hi there. I can't seem to reproduce the problem using similar code to yours: codesandbox.io/s/m788313rvp

– AndyJ
Nov 15 '18 at 17:01





Hi there. I can't seem to reproduce the problem using similar code to yours: codesandbox.io/s/m788313rvp

– AndyJ
Nov 15 '18 at 17:01













Using just this much, we can't reproduce the error. The parent component has few synthetic events and it's too huge to add here. I will try to minimize the code and edit here.

– Akhila Hegde
Nov 15 '18 at 17:18





Using just this much, we can't reproduce the error. The parent component has few synthetic events and it's too huge to add here. I will try to minimize the code and edit here.

– Akhila Hegde
Nov 15 '18 at 17:18













@AndyJ Please see the codesandbox.io/s/y041zknrqv

– Akhila Hegde
Nov 15 '18 at 18:49





@AndyJ Please see the codesandbox.io/s/y041zknrqv

– Akhila Hegde
Nov 15 '18 at 18:49












3 Answers
3






active

oldest

votes


















1














Original codesandbox by OP: https://codesandbox.io/s/y041zknrqv



If the above codesandbox correctly demonstrates your actual problem, then your issue is that you mess up with event.stopPropagation and event.preventDefault



In both Parent1 and Parent2, you provide onClick event handler to the div container and implement event.preventDefault and event.stopPropagation on both handlers. So here is what happens when you click the checkbox the first time:




  • Checkbox receives click event and updates state, resulting in checked
    = true.

  • Event bubbles up to Parent2. Here onClick triggers function handleClick of this component.

  • in handleClick, event.preventDefault stops default action of browser for that event, meaning future click events will not trigger checkbox, while event.stopPropagation prevents parent components from firing onClick, meaning Parent1 will immediately never receive any click event.


And now, from the second click event onwards, only Parent2 can receive and response. Its children and its parents will not handle anything anymore. You can see the console log in here: https://codesandbox.io/s/r75rp285xq



So, to fix your issue, remove event.preventDefault from Parent2's handleClick. If you also want to correct behavior of Parent1, remove event.preventDefault from both components and remove event.stopPropagation from Parent2






share|improve this answer































    0














    Since you are passing the function a parameter, try converting it into an arrow function on your onChange event



    <Checkbox
    checked={this.state.checked}
    onChange={() => this.handleChange('checked')}
    value={user.id}
    color="primary"
    />





    share|improve this answer
























    • No, not helping. Thanks

      – Akhila Hegde
      Nov 15 '18 at 16:01











    • this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

      – AndyJ
      Nov 15 '18 at 17:05



















    0














    If you meant to change the value of checkbox. You must not pass same value onChange in your case



    onChange={(e)=>this.handleChange(e)}


    and your handle change function should be like



    handleChange =event=> {        
    this.setState(prevState=>{
    checked: !prevState.checked
    }, () => {
    if (this.state.checked) {
    this.props.parentMethod1(this.state.checked)
    } else {
    this.props.parentMethod2(this.state.checked)
    }
    });
    };


    This will change your value every onChange event.






    share|improve this answer


























    • I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

      – Akhila Hegde
      Nov 16 '18 at 5:32













    • It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

      – Nilesh Kant
      Nov 16 '18 at 6:42











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323107%2fonchange-event-is-getting-called-only-once-in-react%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Original codesandbox by OP: https://codesandbox.io/s/y041zknrqv



    If the above codesandbox correctly demonstrates your actual problem, then your issue is that you mess up with event.stopPropagation and event.preventDefault



    In both Parent1 and Parent2, you provide onClick event handler to the div container and implement event.preventDefault and event.stopPropagation on both handlers. So here is what happens when you click the checkbox the first time:




    • Checkbox receives click event and updates state, resulting in checked
      = true.

    • Event bubbles up to Parent2. Here onClick triggers function handleClick of this component.

    • in handleClick, event.preventDefault stops default action of browser for that event, meaning future click events will not trigger checkbox, while event.stopPropagation prevents parent components from firing onClick, meaning Parent1 will immediately never receive any click event.


    And now, from the second click event onwards, only Parent2 can receive and response. Its children and its parents will not handle anything anymore. You can see the console log in here: https://codesandbox.io/s/r75rp285xq



    So, to fix your issue, remove event.preventDefault from Parent2's handleClick. If you also want to correct behavior of Parent1, remove event.preventDefault from both components and remove event.stopPropagation from Parent2






    share|improve this answer




























      1














      Original codesandbox by OP: https://codesandbox.io/s/y041zknrqv



      If the above codesandbox correctly demonstrates your actual problem, then your issue is that you mess up with event.stopPropagation and event.preventDefault



      In both Parent1 and Parent2, you provide onClick event handler to the div container and implement event.preventDefault and event.stopPropagation on both handlers. So here is what happens when you click the checkbox the first time:




      • Checkbox receives click event and updates state, resulting in checked
        = true.

      • Event bubbles up to Parent2. Here onClick triggers function handleClick of this component.

      • in handleClick, event.preventDefault stops default action of browser for that event, meaning future click events will not trigger checkbox, while event.stopPropagation prevents parent components from firing onClick, meaning Parent1 will immediately never receive any click event.


      And now, from the second click event onwards, only Parent2 can receive and response. Its children and its parents will not handle anything anymore. You can see the console log in here: https://codesandbox.io/s/r75rp285xq



      So, to fix your issue, remove event.preventDefault from Parent2's handleClick. If you also want to correct behavior of Parent1, remove event.preventDefault from both components and remove event.stopPropagation from Parent2






      share|improve this answer


























        1












        1








        1







        Original codesandbox by OP: https://codesandbox.io/s/y041zknrqv



        If the above codesandbox correctly demonstrates your actual problem, then your issue is that you mess up with event.stopPropagation and event.preventDefault



        In both Parent1 and Parent2, you provide onClick event handler to the div container and implement event.preventDefault and event.stopPropagation on both handlers. So here is what happens when you click the checkbox the first time:




        • Checkbox receives click event and updates state, resulting in checked
          = true.

        • Event bubbles up to Parent2. Here onClick triggers function handleClick of this component.

        • in handleClick, event.preventDefault stops default action of browser for that event, meaning future click events will not trigger checkbox, while event.stopPropagation prevents parent components from firing onClick, meaning Parent1 will immediately never receive any click event.


        And now, from the second click event onwards, only Parent2 can receive and response. Its children and its parents will not handle anything anymore. You can see the console log in here: https://codesandbox.io/s/r75rp285xq



        So, to fix your issue, remove event.preventDefault from Parent2's handleClick. If you also want to correct behavior of Parent1, remove event.preventDefault from both components and remove event.stopPropagation from Parent2






        share|improve this answer













        Original codesandbox by OP: https://codesandbox.io/s/y041zknrqv



        If the above codesandbox correctly demonstrates your actual problem, then your issue is that you mess up with event.stopPropagation and event.preventDefault



        In both Parent1 and Parent2, you provide onClick event handler to the div container and implement event.preventDefault and event.stopPropagation on both handlers. So here is what happens when you click the checkbox the first time:




        • Checkbox receives click event and updates state, resulting in checked
          = true.

        • Event bubbles up to Parent2. Here onClick triggers function handleClick of this component.

        • in handleClick, event.preventDefault stops default action of browser for that event, meaning future click events will not trigger checkbox, while event.stopPropagation prevents parent components from firing onClick, meaning Parent1 will immediately never receive any click event.


        And now, from the second click event onwards, only Parent2 can receive and response. Its children and its parents will not handle anything anymore. You can see the console log in here: https://codesandbox.io/s/r75rp285xq



        So, to fix your issue, remove event.preventDefault from Parent2's handleClick. If you also want to correct behavior of Parent1, remove event.preventDefault from both components and remove event.stopPropagation from Parent2







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 6:40









        blazblaz

        1,701917




        1,701917

























            0














            Since you are passing the function a parameter, try converting it into an arrow function on your onChange event



            <Checkbox
            checked={this.state.checked}
            onChange={() => this.handleChange('checked')}
            value={user.id}
            color="primary"
            />





            share|improve this answer
























            • No, not helping. Thanks

              – Akhila Hegde
              Nov 15 '18 at 16:01











            • this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

              – AndyJ
              Nov 15 '18 at 17:05
















            0














            Since you are passing the function a parameter, try converting it into an arrow function on your onChange event



            <Checkbox
            checked={this.state.checked}
            onChange={() => this.handleChange('checked')}
            value={user.id}
            color="primary"
            />





            share|improve this answer
























            • No, not helping. Thanks

              – Akhila Hegde
              Nov 15 '18 at 16:01











            • this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

              – AndyJ
              Nov 15 '18 at 17:05














            0












            0








            0







            Since you are passing the function a parameter, try converting it into an arrow function on your onChange event



            <Checkbox
            checked={this.state.checked}
            onChange={() => this.handleChange('checked')}
            value={user.id}
            color="primary"
            />





            share|improve this answer













            Since you are passing the function a parameter, try converting it into an arrow function on your onChange event



            <Checkbox
            checked={this.state.checked}
            onChange={() => this.handleChange('checked')}
            value={user.id}
            color="primary"
            />






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 15:52









            DmitriyDmitriy

            612216




            612216













            • No, not helping. Thanks

              – Akhila Hegde
              Nov 15 '18 at 16:01











            • this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

              – AndyJ
              Nov 15 '18 at 17:05



















            • No, not helping. Thanks

              – Akhila Hegde
              Nov 15 '18 at 16:01











            • this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

              – AndyJ
              Nov 15 '18 at 17:05

















            No, not helping. Thanks

            – Akhila Hegde
            Nov 15 '18 at 16:01





            No, not helping. Thanks

            – Akhila Hegde
            Nov 15 '18 at 16:01













            this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

            – AndyJ
            Nov 15 '18 at 17:05





            this.handleChange('checked') creates a handler function, handleChange = name => event => { rather than being the handler function itself. So while this is a good suggestion in general, it doesn't apply to this code.

            – AndyJ
            Nov 15 '18 at 17:05











            0














            If you meant to change the value of checkbox. You must not pass same value onChange in your case



            onChange={(e)=>this.handleChange(e)}


            and your handle change function should be like



            handleChange =event=> {        
            this.setState(prevState=>{
            checked: !prevState.checked
            }, () => {
            if (this.state.checked) {
            this.props.parentMethod1(this.state.checked)
            } else {
            this.props.parentMethod2(this.state.checked)
            }
            });
            };


            This will change your value every onChange event.






            share|improve this answer


























            • I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

              – Akhila Hegde
              Nov 16 '18 at 5:32













            • It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

              – Nilesh Kant
              Nov 16 '18 at 6:42
















            0














            If you meant to change the value of checkbox. You must not pass same value onChange in your case



            onChange={(e)=>this.handleChange(e)}


            and your handle change function should be like



            handleChange =event=> {        
            this.setState(prevState=>{
            checked: !prevState.checked
            }, () => {
            if (this.state.checked) {
            this.props.parentMethod1(this.state.checked)
            } else {
            this.props.parentMethod2(this.state.checked)
            }
            });
            };


            This will change your value every onChange event.






            share|improve this answer


























            • I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

              – Akhila Hegde
              Nov 16 '18 at 5:32













            • It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

              – Nilesh Kant
              Nov 16 '18 at 6:42














            0












            0








            0







            If you meant to change the value of checkbox. You must not pass same value onChange in your case



            onChange={(e)=>this.handleChange(e)}


            and your handle change function should be like



            handleChange =event=> {        
            this.setState(prevState=>{
            checked: !prevState.checked
            }, () => {
            if (this.state.checked) {
            this.props.parentMethod1(this.state.checked)
            } else {
            this.props.parentMethod2(this.state.checked)
            }
            });
            };


            This will change your value every onChange event.






            share|improve this answer















            If you meant to change the value of checkbox. You must not pass same value onChange in your case



            onChange={(e)=>this.handleChange(e)}


            and your handle change function should be like



            handleChange =event=> {        
            this.setState(prevState=>{
            checked: !prevState.checked
            }, () => {
            if (this.state.checked) {
            this.props.parentMethod1(this.state.checked)
            } else {
            this.props.parentMethod2(this.state.checked)
            }
            });
            };


            This will change your value every onChange event.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 5:55

























            answered Nov 16 '18 at 5:24









            Nilesh KantNilesh Kant

            297




            297













            • I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

              – Akhila Hegde
              Nov 16 '18 at 5:32













            • It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

              – Nilesh Kant
              Nov 16 '18 at 6:42



















            • I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

              – Akhila Hegde
              Nov 16 '18 at 5:32













            • It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

              – Nilesh Kant
              Nov 16 '18 at 6:42

















            I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

            – Akhila Hegde
            Nov 16 '18 at 5:32







            I need values to my parent methods. Can you please go through this : codesandbox.io/s/y041zknrqv ? It might clear the question.

            – Akhila Hegde
            Nov 16 '18 at 5:32















            It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

            – Nilesh Kant
            Nov 16 '18 at 6:42





            It is working fine when I just use in that component itself. Your parent components are not updating because you are not pass the value to the parent component. please refer the link codesandbox.io/s/00qpz5vnxl

            – Nilesh Kant
            Nov 16 '18 at 6:42


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323107%2fonchange-event-is-getting-called-only-once-in-react%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine