Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
msudgh opened this issue Jul 2, 2016 · 2 comments
Closed

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

msudgh opened this issue Jul 2, 2016 · 2 comments

Comments

@msudgh
Copy link

msudgh commented Jul 2, 2016

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'));
@maxdeviant
Copy link
Contributor

maxdeviant commented Jul 3, 2016

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.

@msudgh
Copy link
Author

msudgh commented Jul 3, 2016

Oh, that's right.
thanks.

@msudgh msudgh closed this as completed Jul 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants