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

Issues with ComponentDidMount() #272

Open
jguo1002 opened this issue Jul 21, 2019 · 0 comments
Open

Issues with ComponentDidMount() #272

jguo1002 opened this issue Jul 21, 2019 · 0 comments

Comments

@jguo1002
Copy link

Some context: I have a sample-projects.js file, I imported as
import sampleProjects from '../assets/sample-projects';

I tried to load the data and sync with firebase when opening the webpage, so I wrote

Option A

componentWillMount(){
        this.setState({
            projects: sampleProjects
        });
};
componentDidMount(){
        this.ref = base.syncState(`/projects`,{
            context: this,
            state: 'projects'
        });
    }

But nothing happened. Firebase is empty. And the webpage did not load anything. I checked state is empty too.

Then I tried:
Option B

componentWillMount(){
        this.setState({
            projects: sampleProjects
        });
        console.log('will mount:', this.state)
        base.syncState(`/projects`,{
            context: this,
            state: 'projects'
        });
    };

The webpage rendered twice, first time state is normal, second it becomes empty. Firebase is empty too.

So I tried:
Option C

componentWillMount(){
        this.setState({
            projects: sampleProjects
        });
        console.log('will mount:', this.state)
        base.syncState(`/projects`,{
            context: this,
            state: 'projects'
        });
        this.setState({
            projects: sampleProjects
        });
    };

This time it works! Data has been synced to firebase.

Since there is some data in firebase, I used Option A again, and it works nicely.

I kind of solved the problem. But I was wondering why it happens, why Option A did not work the first time?
Thanks!
(PS: Still have some issues, when it renders twice, holder.js does not work. But it is unrelated to this issue. )

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

1 participant