React - Cannot dispatch onChange event from input text element programatically












1














I have two components: ParentComponent and ChildComponent.



The ChildComponent has an input[type="text"] element that when it changes its text that event is propagated to the ParentComponent through the event change and the onChange listener.



The code below is a simplification of a bigger problem, that's why you will see some requirements highlighted there.



My problem is that I need to trigger the change event inside the function: handleClick. I did some experiments with no luck.



Here you have the code sandbox you can experiment with (please provide a fork with your approach):



https://codesandbox.io/s/wqw49j5krw



Here you have the code:



ParentComponent.js



import React from "react";
import ChildComponent from "./ChildComponent";

export default class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "Peter"
};
}
handleChange = event => {
let target = event.target;
let value = target.value;
this.setState({
name: value
});
};
render() {
return (
<div>
<ChildComponent value={this.state.name} onChange={this.handleChange} /><br />
<span>Hello</span>&nbsp;
<span>{this.state.name}!</span>
</div>
);
}
}


ChildComponent.js



import React from "react";
import ReactDOM from "react-dom";

export default class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
value: this.props.value
};
}
handleChange = event => {
const name = event.target.value;
this.setState({ value: name });
if (this.props.onChange !== undefined) {
this.props.onChange(event);
}
};
handleClick = name => {
const inputName = this.refs.name;
console.log('Name before being changed: ' + inputName.value); // this works
// PROBABLY HERE IS WHERE THE CODE NEEDS TO BE MODIFIED
this.setState({ value: name });
var event = new Event('input', { bubbles: true });
inputName.dispatchEvent(event); // this doesn't propagate the event to the parent
};
render() {
return (
<div>
{"Your name: "}
<input type="text"
value={this.state.value}
onChange={this.handleChange}
ref="name"
/>
{/* requirement: the following 2 lines cannot be modified */}
<button onClick={e => { this.handleClick("Steve"); }}>Steve</button>
<button onClick={e => { this.handleClick("Emily"); }}>Emily</button>
</div>
);
}
}


Any idea on how to get this working?



Thanks!










share|improve this question
























  • Your sandbox link is working, what you want to do actually?
    – Just code
    Dec 28 '18 at 5:29










  • when you click the buttons: Steve / Emily the name on the ParentComponent doesn't get updated
    – Viewsonic
    Dec 28 '18 at 5:31










  • Check this stackoverflow.com/questions/23892547/…
    – Praveen Rao Chavan.G
    Dec 28 '18 at 5:37










  • @Justcode the parent component don't get the text underneath updated when on the child component you click the buttons: Steve / Emily. I need the event gets passed to the parent component.
    – Viewsonic
    Dec 28 '18 at 5:43












  • @PraveenRaoChavan.G I tried the approach there but didn't work, here is my try: codesandbox.io/s/1q3mpkmrnl. Could you provide some fork of the code on codesandbox.io?
    – Viewsonic
    Dec 28 '18 at 5:44
















1














I have two components: ParentComponent and ChildComponent.



The ChildComponent has an input[type="text"] element that when it changes its text that event is propagated to the ParentComponent through the event change and the onChange listener.



The code below is a simplification of a bigger problem, that's why you will see some requirements highlighted there.



My problem is that I need to trigger the change event inside the function: handleClick. I did some experiments with no luck.



Here you have the code sandbox you can experiment with (please provide a fork with your approach):



https://codesandbox.io/s/wqw49j5krw



Here you have the code:



ParentComponent.js



import React from "react";
import ChildComponent from "./ChildComponent";

export default class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "Peter"
};
}
handleChange = event => {
let target = event.target;
let value = target.value;
this.setState({
name: value
});
};
render() {
return (
<div>
<ChildComponent value={this.state.name} onChange={this.handleChange} /><br />
<span>Hello</span>&nbsp;
<span>{this.state.name}!</span>
</div>
);
}
}


ChildComponent.js



import React from "react";
import ReactDOM from "react-dom";

export default class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
value: this.props.value
};
}
handleChange = event => {
const name = event.target.value;
this.setState({ value: name });
if (this.props.onChange !== undefined) {
this.props.onChange(event);
}
};
handleClick = name => {
const inputName = this.refs.name;
console.log('Name before being changed: ' + inputName.value); // this works
// PROBABLY HERE IS WHERE THE CODE NEEDS TO BE MODIFIED
this.setState({ value: name });
var event = new Event('input', { bubbles: true });
inputName.dispatchEvent(event); // this doesn't propagate the event to the parent
};
render() {
return (
<div>
{"Your name: "}
<input type="text"
value={this.state.value}
onChange={this.handleChange}
ref="name"
/>
{/* requirement: the following 2 lines cannot be modified */}
<button onClick={e => { this.handleClick("Steve"); }}>Steve</button>
<button onClick={e => { this.handleClick("Emily"); }}>Emily</button>
</div>
);
}
}


Any idea on how to get this working?



Thanks!










share|improve this question
























  • Your sandbox link is working, what you want to do actually?
    – Just code
    Dec 28 '18 at 5:29










  • when you click the buttons: Steve / Emily the name on the ParentComponent doesn't get updated
    – Viewsonic
    Dec 28 '18 at 5:31










  • Check this stackoverflow.com/questions/23892547/…
    – Praveen Rao Chavan.G
    Dec 28 '18 at 5:37










  • @Justcode the parent component don't get the text underneath updated when on the child component you click the buttons: Steve / Emily. I need the event gets passed to the parent component.
    – Viewsonic
    Dec 28 '18 at 5:43












  • @PraveenRaoChavan.G I tried the approach there but didn't work, here is my try: codesandbox.io/s/1q3mpkmrnl. Could you provide some fork of the code on codesandbox.io?
    – Viewsonic
    Dec 28 '18 at 5:44














1












1








1







I have two components: ParentComponent and ChildComponent.



The ChildComponent has an input[type="text"] element that when it changes its text that event is propagated to the ParentComponent through the event change and the onChange listener.



The code below is a simplification of a bigger problem, that's why you will see some requirements highlighted there.



My problem is that I need to trigger the change event inside the function: handleClick. I did some experiments with no luck.



Here you have the code sandbox you can experiment with (please provide a fork with your approach):



https://codesandbox.io/s/wqw49j5krw



Here you have the code:



ParentComponent.js



import React from "react";
import ChildComponent from "./ChildComponent";

export default class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "Peter"
};
}
handleChange = event => {
let target = event.target;
let value = target.value;
this.setState({
name: value
});
};
render() {
return (
<div>
<ChildComponent value={this.state.name} onChange={this.handleChange} /><br />
<span>Hello</span>&nbsp;
<span>{this.state.name}!</span>
</div>
);
}
}


ChildComponent.js



import React from "react";
import ReactDOM from "react-dom";

export default class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
value: this.props.value
};
}
handleChange = event => {
const name = event.target.value;
this.setState({ value: name });
if (this.props.onChange !== undefined) {
this.props.onChange(event);
}
};
handleClick = name => {
const inputName = this.refs.name;
console.log('Name before being changed: ' + inputName.value); // this works
// PROBABLY HERE IS WHERE THE CODE NEEDS TO BE MODIFIED
this.setState({ value: name });
var event = new Event('input', { bubbles: true });
inputName.dispatchEvent(event); // this doesn't propagate the event to the parent
};
render() {
return (
<div>
{"Your name: "}
<input type="text"
value={this.state.value}
onChange={this.handleChange}
ref="name"
/>
{/* requirement: the following 2 lines cannot be modified */}
<button onClick={e => { this.handleClick("Steve"); }}>Steve</button>
<button onClick={e => { this.handleClick("Emily"); }}>Emily</button>
</div>
);
}
}


Any idea on how to get this working?



Thanks!










share|improve this question















I have two components: ParentComponent and ChildComponent.



The ChildComponent has an input[type="text"] element that when it changes its text that event is propagated to the ParentComponent through the event change and the onChange listener.



The code below is a simplification of a bigger problem, that's why you will see some requirements highlighted there.



My problem is that I need to trigger the change event inside the function: handleClick. I did some experiments with no luck.



Here you have the code sandbox you can experiment with (please provide a fork with your approach):



https://codesandbox.io/s/wqw49j5krw



Here you have the code:



ParentComponent.js



import React from "react";
import ChildComponent from "./ChildComponent";

export default class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "Peter"
};
}
handleChange = event => {
let target = event.target;
let value = target.value;
this.setState({
name: value
});
};
render() {
return (
<div>
<ChildComponent value={this.state.name} onChange={this.handleChange} /><br />
<span>Hello</span>&nbsp;
<span>{this.state.name}!</span>
</div>
);
}
}


ChildComponent.js



import React from "react";
import ReactDOM from "react-dom";

export default class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
value: this.props.value
};
}
handleChange = event => {
const name = event.target.value;
this.setState({ value: name });
if (this.props.onChange !== undefined) {
this.props.onChange(event);
}
};
handleClick = name => {
const inputName = this.refs.name;
console.log('Name before being changed: ' + inputName.value); // this works
// PROBABLY HERE IS WHERE THE CODE NEEDS TO BE MODIFIED
this.setState({ value: name });
var event = new Event('input', { bubbles: true });
inputName.dispatchEvent(event); // this doesn't propagate the event to the parent
};
render() {
return (
<div>
{"Your name: "}
<input type="text"
value={this.state.value}
onChange={this.handleChange}
ref="name"
/>
{/* requirement: the following 2 lines cannot be modified */}
<button onClick={e => { this.handleClick("Steve"); }}>Steve</button>
<button onClick={e => { this.handleClick("Emily"); }}>Emily</button>
</div>
);
}
}


Any idea on how to get this working?



Thanks!







javascript html html5 reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 5:36







Viewsonic

















asked Dec 28 '18 at 5:26









ViewsonicViewsonic

867




867












  • Your sandbox link is working, what you want to do actually?
    – Just code
    Dec 28 '18 at 5:29










  • when you click the buttons: Steve / Emily the name on the ParentComponent doesn't get updated
    – Viewsonic
    Dec 28 '18 at 5:31










  • Check this stackoverflow.com/questions/23892547/…
    – Praveen Rao Chavan.G
    Dec 28 '18 at 5:37










  • @Justcode the parent component don't get the text underneath updated when on the child component you click the buttons: Steve / Emily. I need the event gets passed to the parent component.
    – Viewsonic
    Dec 28 '18 at 5:43












  • @PraveenRaoChavan.G I tried the approach there but didn't work, here is my try: codesandbox.io/s/1q3mpkmrnl. Could you provide some fork of the code on codesandbox.io?
    – Viewsonic
    Dec 28 '18 at 5:44


















  • Your sandbox link is working, what you want to do actually?
    – Just code
    Dec 28 '18 at 5:29










  • when you click the buttons: Steve / Emily the name on the ParentComponent doesn't get updated
    – Viewsonic
    Dec 28 '18 at 5:31










  • Check this stackoverflow.com/questions/23892547/…
    – Praveen Rao Chavan.G
    Dec 28 '18 at 5:37










  • @Justcode the parent component don't get the text underneath updated when on the child component you click the buttons: Steve / Emily. I need the event gets passed to the parent component.
    – Viewsonic
    Dec 28 '18 at 5:43












  • @PraveenRaoChavan.G I tried the approach there but didn't work, here is my try: codesandbox.io/s/1q3mpkmrnl. Could you provide some fork of the code on codesandbox.io?
    – Viewsonic
    Dec 28 '18 at 5:44
















Your sandbox link is working, what you want to do actually?
– Just code
Dec 28 '18 at 5:29




Your sandbox link is working, what you want to do actually?
– Just code
Dec 28 '18 at 5:29












when you click the buttons: Steve / Emily the name on the ParentComponent doesn't get updated
– Viewsonic
Dec 28 '18 at 5:31




when you click the buttons: Steve / Emily the name on the ParentComponent doesn't get updated
– Viewsonic
Dec 28 '18 at 5:31












Check this stackoverflow.com/questions/23892547/…
– Praveen Rao Chavan.G
Dec 28 '18 at 5:37




Check this stackoverflow.com/questions/23892547/…
– Praveen Rao Chavan.G
Dec 28 '18 at 5:37












@Justcode the parent component don't get the text underneath updated when on the child component you click the buttons: Steve / Emily. I need the event gets passed to the parent component.
– Viewsonic
Dec 28 '18 at 5:43






@Justcode the parent component don't get the text underneath updated when on the child component you click the buttons: Steve / Emily. I need the event gets passed to the parent component.
– Viewsonic
Dec 28 '18 at 5:43














@PraveenRaoChavan.G I tried the approach there but didn't work, here is my try: codesandbox.io/s/1q3mpkmrnl. Could you provide some fork of the code on codesandbox.io?
– Viewsonic
Dec 28 '18 at 5:44




@PraveenRaoChavan.G I tried the approach there but didn't work, here is my try: codesandbox.io/s/1q3mpkmrnl. Could you provide some fork of the code on codesandbox.io?
– Viewsonic
Dec 28 '18 at 5:44












4 Answers
4






active

oldest

votes


















1














You are missing the track of the input change,




Because React tracks when you set the value property on an input to
keep track of the node's value. When you dispatch a change event, it
checks it's last value against the current value and if they're the
same it does not call any event handlers (as no change has taken place
as far as react is concerned). So we have to set the value in a way
that React's value setter function will not be called, which is where
the setNativeValue comes into play.




Here you are setting the state instead of changing the input's value directly so, it will not get the updated value when you are dispatching the event. and if you write value directly like input.value it can not track the changes of the input.



so, you should set the value and dispatch the event, this way you can have the updated value when event is dispatched.



Here is the link of the reference and another, there are other ways too, to fire the change event.



Here is the function you need to set the property so that react can track the changes,



  handleClick = name => {
const inputName = this.refs.name;
console.log("Name before being changed: " + inputName.value); // this works

var event = new Event("input", { bubbles: true });

this.setNativeValue(inputName, name);
inputName.dispatchEvent(event);
};

setNativeValue = (element, value) => {
const valueSetter = Object.getOwnPropertyDescriptor(element, "value").set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(
prototype,
"value"
).set;

if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}
};


Demo






share|improve this answer



















  • 1




    that did the trick, good answer!
    – Viewsonic
    Dec 28 '18 at 6:43










  • @Viewsonic You can accept this as the answer :)
    – Surya Purohit
    Dec 28 '18 at 6:49



















0














You can include an onClick function to the parent component to change the state and pass it onto the child component. In the child component you can call the onClick method passed down via props and pass the name when button is clicked.



You can find the example code in the link provided below.



https://codesandbox.io/s/k0n1j2rrx7






share|improve this answer





























    0














    I corrected your code like that:
    in handleChange function of parentComponent I changed the parameter to value from event.
    In childComponent I added the function call below to handleClick function:



     this.props.onChange(name);


    Why I did these changes because, you call your parentComponent's handleChange function from your childComponent' s onChange function.
    When you click the button, it calls handleClick function. In handleClick function it calls property onChange function. The props.onChange function calls handleChange function of parent component.
    handleClick -> this.props.onChange(name) -> this.handleChange(name)
    I hope it helps.






    share|improve this answer































      0














      If I understood correctly, you are trying to change the parent text like Hello Peter when you click the button from child component.



      Check my codesandbox : codesandbox



      Changes:



      In child component, pass a props like this.props.onClick(name);



      In parent, get it like



        handleClick = name => {
      this.setState({
      name
      });
      };


      Hope this is will help you.






      share|improve this answer























        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%2f53954029%2freact-cannot-dispatch-onchange-event-from-input-text-element-programatically%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        You are missing the track of the input change,




        Because React tracks when you set the value property on an input to
        keep track of the node's value. When you dispatch a change event, it
        checks it's last value against the current value and if they're the
        same it does not call any event handlers (as no change has taken place
        as far as react is concerned). So we have to set the value in a way
        that React's value setter function will not be called, which is where
        the setNativeValue comes into play.




        Here you are setting the state instead of changing the input's value directly so, it will not get the updated value when you are dispatching the event. and if you write value directly like input.value it can not track the changes of the input.



        so, you should set the value and dispatch the event, this way you can have the updated value when event is dispatched.



        Here is the link of the reference and another, there are other ways too, to fire the change event.



        Here is the function you need to set the property so that react can track the changes,



          handleClick = name => {
        const inputName = this.refs.name;
        console.log("Name before being changed: " + inputName.value); // this works

        var event = new Event("input", { bubbles: true });

        this.setNativeValue(inputName, name);
        inputName.dispatchEvent(event);
        };

        setNativeValue = (element, value) => {
        const valueSetter = Object.getOwnPropertyDescriptor(element, "value").set;
        const prototype = Object.getPrototypeOf(element);
        const prototypeValueSetter = Object.getOwnPropertyDescriptor(
        prototype,
        "value"
        ).set;

        if (valueSetter && valueSetter !== prototypeValueSetter) {
        prototypeValueSetter.call(element, value);
        } else {
        valueSetter.call(element, value);
        }
        };


        Demo






        share|improve this answer



















        • 1




          that did the trick, good answer!
          – Viewsonic
          Dec 28 '18 at 6:43










        • @Viewsonic You can accept this as the answer :)
          – Surya Purohit
          Dec 28 '18 at 6:49
















        1














        You are missing the track of the input change,




        Because React tracks when you set the value property on an input to
        keep track of the node's value. When you dispatch a change event, it
        checks it's last value against the current value and if they're the
        same it does not call any event handlers (as no change has taken place
        as far as react is concerned). So we have to set the value in a way
        that React's value setter function will not be called, which is where
        the setNativeValue comes into play.




        Here you are setting the state instead of changing the input's value directly so, it will not get the updated value when you are dispatching the event. and if you write value directly like input.value it can not track the changes of the input.



        so, you should set the value and dispatch the event, this way you can have the updated value when event is dispatched.



        Here is the link of the reference and another, there are other ways too, to fire the change event.



        Here is the function you need to set the property so that react can track the changes,



          handleClick = name => {
        const inputName = this.refs.name;
        console.log("Name before being changed: " + inputName.value); // this works

        var event = new Event("input", { bubbles: true });

        this.setNativeValue(inputName, name);
        inputName.dispatchEvent(event);
        };

        setNativeValue = (element, value) => {
        const valueSetter = Object.getOwnPropertyDescriptor(element, "value").set;
        const prototype = Object.getPrototypeOf(element);
        const prototypeValueSetter = Object.getOwnPropertyDescriptor(
        prototype,
        "value"
        ).set;

        if (valueSetter && valueSetter !== prototypeValueSetter) {
        prototypeValueSetter.call(element, value);
        } else {
        valueSetter.call(element, value);
        }
        };


        Demo






        share|improve this answer



















        • 1




          that did the trick, good answer!
          – Viewsonic
          Dec 28 '18 at 6:43










        • @Viewsonic You can accept this as the answer :)
          – Surya Purohit
          Dec 28 '18 at 6:49














        1












        1








        1






        You are missing the track of the input change,




        Because React tracks when you set the value property on an input to
        keep track of the node's value. When you dispatch a change event, it
        checks it's last value against the current value and if they're the
        same it does not call any event handlers (as no change has taken place
        as far as react is concerned). So we have to set the value in a way
        that React's value setter function will not be called, which is where
        the setNativeValue comes into play.




        Here you are setting the state instead of changing the input's value directly so, it will not get the updated value when you are dispatching the event. and if you write value directly like input.value it can not track the changes of the input.



        so, you should set the value and dispatch the event, this way you can have the updated value when event is dispatched.



        Here is the link of the reference and another, there are other ways too, to fire the change event.



        Here is the function you need to set the property so that react can track the changes,



          handleClick = name => {
        const inputName = this.refs.name;
        console.log("Name before being changed: " + inputName.value); // this works

        var event = new Event("input", { bubbles: true });

        this.setNativeValue(inputName, name);
        inputName.dispatchEvent(event);
        };

        setNativeValue = (element, value) => {
        const valueSetter = Object.getOwnPropertyDescriptor(element, "value").set;
        const prototype = Object.getPrototypeOf(element);
        const prototypeValueSetter = Object.getOwnPropertyDescriptor(
        prototype,
        "value"
        ).set;

        if (valueSetter && valueSetter !== prototypeValueSetter) {
        prototypeValueSetter.call(element, value);
        } else {
        valueSetter.call(element, value);
        }
        };


        Demo






        share|improve this answer














        You are missing the track of the input change,




        Because React tracks when you set the value property on an input to
        keep track of the node's value. When you dispatch a change event, it
        checks it's last value against the current value and if they're the
        same it does not call any event handlers (as no change has taken place
        as far as react is concerned). So we have to set the value in a way
        that React's value setter function will not be called, which is where
        the setNativeValue comes into play.




        Here you are setting the state instead of changing the input's value directly so, it will not get the updated value when you are dispatching the event. and if you write value directly like input.value it can not track the changes of the input.



        so, you should set the value and dispatch the event, this way you can have the updated value when event is dispatched.



        Here is the link of the reference and another, there are other ways too, to fire the change event.



        Here is the function you need to set the property so that react can track the changes,



          handleClick = name => {
        const inputName = this.refs.name;
        console.log("Name before being changed: " + inputName.value); // this works

        var event = new Event("input", { bubbles: true });

        this.setNativeValue(inputName, name);
        inputName.dispatchEvent(event);
        };

        setNativeValue = (element, value) => {
        const valueSetter = Object.getOwnPropertyDescriptor(element, "value").set;
        const prototype = Object.getPrototypeOf(element);
        const prototypeValueSetter = Object.getOwnPropertyDescriptor(
        prototype,
        "value"
        ).set;

        if (valueSetter && valueSetter !== prototypeValueSetter) {
        prototypeValueSetter.call(element, value);
        } else {
        valueSetter.call(element, value);
        }
        };


        Demo







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 28 '18 at 6:10

























        answered Dec 28 '18 at 6:01









        Just codeJust code

        9,46642966




        9,46642966








        • 1




          that did the trick, good answer!
          – Viewsonic
          Dec 28 '18 at 6:43










        • @Viewsonic You can accept this as the answer :)
          – Surya Purohit
          Dec 28 '18 at 6:49














        • 1




          that did the trick, good answer!
          – Viewsonic
          Dec 28 '18 at 6:43










        • @Viewsonic You can accept this as the answer :)
          – Surya Purohit
          Dec 28 '18 at 6:49








        1




        1




        that did the trick, good answer!
        – Viewsonic
        Dec 28 '18 at 6:43




        that did the trick, good answer!
        – Viewsonic
        Dec 28 '18 at 6:43












        @Viewsonic You can accept this as the answer :)
        – Surya Purohit
        Dec 28 '18 at 6:49




        @Viewsonic You can accept this as the answer :)
        – Surya Purohit
        Dec 28 '18 at 6:49













        0














        You can include an onClick function to the parent component to change the state and pass it onto the child component. In the child component you can call the onClick method passed down via props and pass the name when button is clicked.



        You can find the example code in the link provided below.



        https://codesandbox.io/s/k0n1j2rrx7






        share|improve this answer


























          0














          You can include an onClick function to the parent component to change the state and pass it onto the child component. In the child component you can call the onClick method passed down via props and pass the name when button is clicked.



          You can find the example code in the link provided below.



          https://codesandbox.io/s/k0n1j2rrx7






          share|improve this answer
























            0












            0








            0






            You can include an onClick function to the parent component to change the state and pass it onto the child component. In the child component you can call the onClick method passed down via props and pass the name when button is clicked.



            You can find the example code in the link provided below.



            https://codesandbox.io/s/k0n1j2rrx7






            share|improve this answer












            You can include an onClick function to the parent component to change the state and pass it onto the child component. In the child component you can call the onClick method passed down via props and pass the name when button is clicked.



            You can find the example code in the link provided below.



            https://codesandbox.io/s/k0n1j2rrx7







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 28 '18 at 5:54









            MuljayanMuljayan

            596




            596























                0














                I corrected your code like that:
                in handleChange function of parentComponent I changed the parameter to value from event.
                In childComponent I added the function call below to handleClick function:



                 this.props.onChange(name);


                Why I did these changes because, you call your parentComponent's handleChange function from your childComponent' s onChange function.
                When you click the button, it calls handleClick function. In handleClick function it calls property onChange function. The props.onChange function calls handleChange function of parent component.
                handleClick -> this.props.onChange(name) -> this.handleChange(name)
                I hope it helps.






                share|improve this answer




























                  0














                  I corrected your code like that:
                  in handleChange function of parentComponent I changed the parameter to value from event.
                  In childComponent I added the function call below to handleClick function:



                   this.props.onChange(name);


                  Why I did these changes because, you call your parentComponent's handleChange function from your childComponent' s onChange function.
                  When you click the button, it calls handleClick function. In handleClick function it calls property onChange function. The props.onChange function calls handleChange function of parent component.
                  handleClick -> this.props.onChange(name) -> this.handleChange(name)
                  I hope it helps.






                  share|improve this answer


























                    0












                    0








                    0






                    I corrected your code like that:
                    in handleChange function of parentComponent I changed the parameter to value from event.
                    In childComponent I added the function call below to handleClick function:



                     this.props.onChange(name);


                    Why I did these changes because, you call your parentComponent's handleChange function from your childComponent' s onChange function.
                    When you click the button, it calls handleClick function. In handleClick function it calls property onChange function. The props.onChange function calls handleChange function of parent component.
                    handleClick -> this.props.onChange(name) -> this.handleChange(name)
                    I hope it helps.






                    share|improve this answer














                    I corrected your code like that:
                    in handleChange function of parentComponent I changed the parameter to value from event.
                    In childComponent I added the function call below to handleClick function:



                     this.props.onChange(name);


                    Why I did these changes because, you call your parentComponent's handleChange function from your childComponent' s onChange function.
                    When you click the button, it calls handleClick function. In handleClick function it calls property onChange function. The props.onChange function calls handleChange function of parent component.
                    handleClick -> this.props.onChange(name) -> this.handleChange(name)
                    I hope it helps.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 28 '18 at 6:16

























                    answered Dec 28 '18 at 6:09









                    mstfyldzmstfyldz

                    1044




                    1044























                        0














                        If I understood correctly, you are trying to change the parent text like Hello Peter when you click the button from child component.



                        Check my codesandbox : codesandbox



                        Changes:



                        In child component, pass a props like this.props.onClick(name);



                        In parent, get it like



                          handleClick = name => {
                        this.setState({
                        name
                        });
                        };


                        Hope this is will help you.






                        share|improve this answer




























                          0














                          If I understood correctly, you are trying to change the parent text like Hello Peter when you click the button from child component.



                          Check my codesandbox : codesandbox



                          Changes:



                          In child component, pass a props like this.props.onClick(name);



                          In parent, get it like



                            handleClick = name => {
                          this.setState({
                          name
                          });
                          };


                          Hope this is will help you.






                          share|improve this answer


























                            0












                            0








                            0






                            If I understood correctly, you are trying to change the parent text like Hello Peter when you click the button from child component.



                            Check my codesandbox : codesandbox



                            Changes:



                            In child component, pass a props like this.props.onClick(name);



                            In parent, get it like



                              handleClick = name => {
                            this.setState({
                            name
                            });
                            };


                            Hope this is will help you.






                            share|improve this answer














                            If I understood correctly, you are trying to change the parent text like Hello Peter when you click the button from child component.



                            Check my codesandbox : codesandbox



                            Changes:



                            In child component, pass a props like this.props.onClick(name);



                            In parent, get it like



                              handleClick = name => {
                            this.setState({
                            name
                            });
                            };


                            Hope this is will help you.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 28 '18 at 6:24









                            Sangeeth

                            558




                            558










                            answered Dec 28 '18 at 5:51









                            JayavelJayavel

                            1,039820




                            1,039820






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f53954029%2freact-cannot-dispatch-onchange-event-from-input-text-element-programatically%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

                                Mossoró

                                Error while reading .h5 file using the rhdf5 package in R

                                Pushsharp Apns notification error: 'InvalidToken'