Skip to content

setState(...): Cannot update during an existing state transition #7177

Closed
@masoudgs

Description

@masoudgs

hi guys,
i have a component by which count character, word a div.
now, when component will be render i got a lot duplicate warning in chrome console tool => Warning: setState(...): Cannot update during an existing state transition
what can i do for fix this problem?
finally, i want return states.
thanks

import React from 'react';
import ReactDOM from 'react-dom';
import Countable from 'countable';


class Statics extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            paragraphs: 0,
            sentences: 0,
            words: 0,
            characters: 0,
            all: 0
        };
        this.Counter_Callback = this.Counter_Callback.bind(this);
    }
    Counter_Callback(counter) {
        this.setState({
            paragraphs: counter.paragraph,
            sentences: counter.sentences,
            words: counter.words,
            characters: counter.characters,
            all: counter.all
        });
    }
    render() {
        var area = document.getElementById('editor');
        Countable.live(area, this.Counter_Callback);
        return (
            <div>
                <div id="Word_Counter">
                    {this.state.words} Words
                </div>
            </div>
        );
    }
}

ReactDOM.render(<Statics/>, document.getElementById('footer-statics'));

Activity

maxdeviant

maxdeviant commented on Jul 3, 2016

@maxdeviant
Contributor

You are using this.Counter_Callback, which updates your component state, inside of render().

You should move

var area = document.getElementById('editor');
Countable.live(area, this.Counter_Callback);

into one of the React lifecycle methods (probably componentDidMount). See the docs for details.

masoudgs

masoudgs commented on Jul 3, 2016

@masoudgs
Author

Oh, that's right.
thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @maxdeviant@masoudgs

        Issue actions

          setState(...): Cannot update during an existing state transition · Issue #7177 · facebook/react