Why does input setState inside setTimeout break Chinese IME?












0















I have a simple controlled input component in React and I am trying to wrap my head around this behavior.



If I do it the normal way, everything works fine and Chinese IME works as expected:



<input
type="text"
value={this.state.value}
onChange={(event) => {
this.setState({ value: event.target.value });
}}
/>


But if I wrapped a setTimeout or another setState outside, Chinese IME does not work anymore, it seems like it's been interrupted during each setState?



# setTimeout
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
setTimeout(() => {
this.setState({ value });
}, 0);
}}
/>

# another setState
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
this.setState({ isTyping: true }, () => {
this.setState({ value });
});
}}
/>


I am not looking for the correct way to make Chinese IME work, I just find it very interesting and want to understand what causes this behavior. I sort of understand there's an "update loop" for each setState operation but I am not sure how that would break the Chinese IME. Is this related to event loop and how setTimeout pushes the operation to the back of the queue?



Many thanks!










share|improve this question























  • it should check the working code

    – Mayank Shukla
    Nov 15 '18 at 3:46
















0















I have a simple controlled input component in React and I am trying to wrap my head around this behavior.



If I do it the normal way, everything works fine and Chinese IME works as expected:



<input
type="text"
value={this.state.value}
onChange={(event) => {
this.setState({ value: event.target.value });
}}
/>


But if I wrapped a setTimeout or another setState outside, Chinese IME does not work anymore, it seems like it's been interrupted during each setState?



# setTimeout
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
setTimeout(() => {
this.setState({ value });
}, 0);
}}
/>

# another setState
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
this.setState({ isTyping: true }, () => {
this.setState({ value });
});
}}
/>


I am not looking for the correct way to make Chinese IME work, I just find it very interesting and want to understand what causes this behavior. I sort of understand there's an "update loop" for each setState operation but I am not sure how that would break the Chinese IME. Is this related to event loop and how setTimeout pushes the operation to the back of the queue?



Many thanks!










share|improve this question























  • it should check the working code

    – Mayank Shukla
    Nov 15 '18 at 3:46














0












0








0








I have a simple controlled input component in React and I am trying to wrap my head around this behavior.



If I do it the normal way, everything works fine and Chinese IME works as expected:



<input
type="text"
value={this.state.value}
onChange={(event) => {
this.setState({ value: event.target.value });
}}
/>


But if I wrapped a setTimeout or another setState outside, Chinese IME does not work anymore, it seems like it's been interrupted during each setState?



# setTimeout
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
setTimeout(() => {
this.setState({ value });
}, 0);
}}
/>

# another setState
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
this.setState({ isTyping: true }, () => {
this.setState({ value });
});
}}
/>


I am not looking for the correct way to make Chinese IME work, I just find it very interesting and want to understand what causes this behavior. I sort of understand there's an "update loop" for each setState operation but I am not sure how that would break the Chinese IME. Is this related to event loop and how setTimeout pushes the operation to the back of the queue?



Many thanks!










share|improve this question














I have a simple controlled input component in React and I am trying to wrap my head around this behavior.



If I do it the normal way, everything works fine and Chinese IME works as expected:



<input
type="text"
value={this.state.value}
onChange={(event) => {
this.setState({ value: event.target.value });
}}
/>


But if I wrapped a setTimeout or another setState outside, Chinese IME does not work anymore, it seems like it's been interrupted during each setState?



# setTimeout
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
setTimeout(() => {
this.setState({ value });
}, 0);
}}
/>

# another setState
<input
type="text"
value={this.state.value}
onChange={(event) => {
const value = event.target.value;
this.setState({ isTyping: true }, () => {
this.setState({ value });
});
}}
/>


I am not looking for the correct way to make Chinese IME work, I just find it very interesting and want to understand what causes this behavior. I sort of understand there's an "update loop" for each setState operation but I am not sure how that would break the Chinese IME. Is this related to event loop and how setTimeout pushes the operation to the back of the queue?



Many thanks!







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 3:38









colloquecolloque

333




333













  • it should check the working code

    – Mayank Shukla
    Nov 15 '18 at 3:46



















  • it should check the working code

    – Mayank Shukla
    Nov 15 '18 at 3:46

















it should check the working code

– Mayank Shukla
Nov 15 '18 at 3:46





it should check the working code

– Mayank Shukla
Nov 15 '18 at 3:46












2 Answers
2






active

oldest

votes


















0














Try this. You are not recommended to do setTimeout in user input event handler function. Don’t use setTimeout of setting state



   handleChange = event = > {
const value = event.target.value;
this.setState({ value });
}

<input
type="text"
value={this.state.value}
onChange={event => this.handleChange(event)}
/>


Even if it is 0 milliseconds setTimeout will delay setting user input. Check below thread for more details about using setTimeout with zero milliseconds






share|improve this answer

































    0














     I checked your code amd I didn't found any issue on your code. 
    My react version is 16.6 and your code work fine on my machine.

    import React from 'react';
    class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    value: ''
    }
    }

    onClick() {
    this.setState({ showLazy: !this.state.showLazy });
    }

    render() {

    return (<div>
    <input
    type="text"
    value={this.state.value}
    onChange={(event) => {
    const value = event.target.value;
    this.setState({ isTyping: true }, () => {
    this.setState({ value });
    });
    }}
    />
    <input
    type="text"
    value={this.state.value}
    onChange={event => {
    const value = event.target.value;
    setTimeout(() => {
    this.setState({ value })
    }, 0);
    }}
    />
    </div>)
    }
    }


    export default AdvanceApp;






    share|improve this answer


























    • Add some description as well with you answer.

      – Busy Bee
      Nov 15 '18 at 4:36











    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%2f53312079%2fwhy-does-input-setstate-inside-settimeout-break-chinese-ime%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try this. You are not recommended to do setTimeout in user input event handler function. Don’t use setTimeout of setting state



       handleChange = event = > {
    const value = event.target.value;
    this.setState({ value });
    }

    <input
    type="text"
    value={this.state.value}
    onChange={event => this.handleChange(event)}
    />


    Even if it is 0 milliseconds setTimeout will delay setting user input. Check below thread for more details about using setTimeout with zero milliseconds






    share|improve this answer






























      0














      Try this. You are not recommended to do setTimeout in user input event handler function. Don’t use setTimeout of setting state



         handleChange = event = > {
      const value = event.target.value;
      this.setState({ value });
      }

      <input
      type="text"
      value={this.state.value}
      onChange={event => this.handleChange(event)}
      />


      Even if it is 0 milliseconds setTimeout will delay setting user input. Check below thread for more details about using setTimeout with zero milliseconds






      share|improve this answer




























        0












        0








        0







        Try this. You are not recommended to do setTimeout in user input event handler function. Don’t use setTimeout of setting state



           handleChange = event = > {
        const value = event.target.value;
        this.setState({ value });
        }

        <input
        type="text"
        value={this.state.value}
        onChange={event => this.handleChange(event)}
        />


        Even if it is 0 milliseconds setTimeout will delay setting user input. Check below thread for more details about using setTimeout with zero milliseconds






        share|improve this answer















        Try this. You are not recommended to do setTimeout in user input event handler function. Don’t use setTimeout of setting state



           handleChange = event = > {
        const value = event.target.value;
        this.setState({ value });
        }

        <input
        type="text"
        value={this.state.value}
        onChange={event => this.handleChange(event)}
        />


        Even if it is 0 milliseconds setTimeout will delay setting user input. Check below thread for more details about using setTimeout with zero milliseconds







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 4:48

























        answered Nov 15 '18 at 4:20









        Hemadri DasariHemadri Dasari

        9,75531848




        9,75531848

























            0














             I checked your code amd I didn't found any issue on your code. 
            My react version is 16.6 and your code work fine on my machine.

            import React from 'react';
            class App extends React.Component {
            constructor(props) {
            super(props);
            this.state = {
            value: ''
            }
            }

            onClick() {
            this.setState({ showLazy: !this.state.showLazy });
            }

            render() {

            return (<div>
            <input
            type="text"
            value={this.state.value}
            onChange={(event) => {
            const value = event.target.value;
            this.setState({ isTyping: true }, () => {
            this.setState({ value });
            });
            }}
            />
            <input
            type="text"
            value={this.state.value}
            onChange={event => {
            const value = event.target.value;
            setTimeout(() => {
            this.setState({ value })
            }, 0);
            }}
            />
            </div>)
            }
            }


            export default AdvanceApp;






            share|improve this answer


























            • Add some description as well with you answer.

              – Busy Bee
              Nov 15 '18 at 4:36
















            0














             I checked your code amd I didn't found any issue on your code. 
            My react version is 16.6 and your code work fine on my machine.

            import React from 'react';
            class App extends React.Component {
            constructor(props) {
            super(props);
            this.state = {
            value: ''
            }
            }

            onClick() {
            this.setState({ showLazy: !this.state.showLazy });
            }

            render() {

            return (<div>
            <input
            type="text"
            value={this.state.value}
            onChange={(event) => {
            const value = event.target.value;
            this.setState({ isTyping: true }, () => {
            this.setState({ value });
            });
            }}
            />
            <input
            type="text"
            value={this.state.value}
            onChange={event => {
            const value = event.target.value;
            setTimeout(() => {
            this.setState({ value })
            }, 0);
            }}
            />
            </div>)
            }
            }


            export default AdvanceApp;






            share|improve this answer


























            • Add some description as well with you answer.

              – Busy Bee
              Nov 15 '18 at 4:36














            0












            0








            0







             I checked your code amd I didn't found any issue on your code. 
            My react version is 16.6 and your code work fine on my machine.

            import React from 'react';
            class App extends React.Component {
            constructor(props) {
            super(props);
            this.state = {
            value: ''
            }
            }

            onClick() {
            this.setState({ showLazy: !this.state.showLazy });
            }

            render() {

            return (<div>
            <input
            type="text"
            value={this.state.value}
            onChange={(event) => {
            const value = event.target.value;
            this.setState({ isTyping: true }, () => {
            this.setState({ value });
            });
            }}
            />
            <input
            type="text"
            value={this.state.value}
            onChange={event => {
            const value = event.target.value;
            setTimeout(() => {
            this.setState({ value })
            }, 0);
            }}
            />
            </div>)
            }
            }


            export default AdvanceApp;






            share|improve this answer















             I checked your code amd I didn't found any issue on your code. 
            My react version is 16.6 and your code work fine on my machine.

            import React from 'react';
            class App extends React.Component {
            constructor(props) {
            super(props);
            this.state = {
            value: ''
            }
            }

            onClick() {
            this.setState({ showLazy: !this.state.showLazy });
            }

            render() {

            return (<div>
            <input
            type="text"
            value={this.state.value}
            onChange={(event) => {
            const value = event.target.value;
            this.setState({ isTyping: true }, () => {
            this.setState({ value });
            });
            }}
            />
            <input
            type="text"
            value={this.state.value}
            onChange={event => {
            const value = event.target.value;
            setTimeout(() => {
            this.setState({ value })
            }, 0);
            }}
            />
            </div>)
            }
            }


            export default AdvanceApp;







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 5:35

























            answered Nov 15 '18 at 4:26









            Biplab MalakarBiplab Malakar

            42837




            42837













            • Add some description as well with you answer.

              – Busy Bee
              Nov 15 '18 at 4:36



















            • Add some description as well with you answer.

              – Busy Bee
              Nov 15 '18 at 4:36

















            Add some description as well with you answer.

            – Busy Bee
            Nov 15 '18 at 4:36





            Add some description as well with you answer.

            – Busy Bee
            Nov 15 '18 at 4:36


















            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%2f53312079%2fwhy-does-input-setstate-inside-settimeout-break-chinese-ime%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values