GithubHelp home page GithubHelp logo

Comments (11)

frankzang avatar frankzang commented on May 20, 2024 3

It is more advised to just use the [] argument option, like:

useEffect(() => {
   // actions here
}, [])

from easy-peasy.

frankzang avatar frankzang commented on May 20, 2024 2

You could try something like this:

fetch: effect(async (dispatch, payload, getState) => {
const articleCollection = firebase.firestore().collection("article");

const articles = await articleCollection.get();

const saved = [];

articles.forEach(function (doc) {
    saved.push(doc.data());
});
dispatch.article.fetched(saved);

})

from easy-peasy.

frankzang avatar frankzang commented on May 20, 2024

Your integration with firebase isn't really correct, the forEach method that you area calling doesn't return to the "then" method, you have to set an accumulator and then pass it to the dispatcher. Also, you're probably dispatching before the articles are fetched.

from easy-peasy.

vemundeldegard avatar vemundeldegard commented on May 20, 2024

You could try something like this:

fetch: effect(async (dispatch, payload, getState) => {
const articleCollection = firebase.firestore().collection("article");

const articles = await articleCollection.get();

const saved = [];

articles.forEach(function (doc) {
    saved.push(doc.data());
});
dispatch.article.fetched(saved);

})

Thanks, that was smart! I tried that, but I still get an endless loop and no data shown when trying to render. Also tried to console.log the state... endless loop here too.

const articles = useStore(state => state.article.items);
useEffect(() => {
    console.log(articles)
});

from easy-peasy.

frankzang avatar frankzang commented on May 20, 2024

Where are you calling the "initialise" method?

from easy-peasy.

vemundeldegard avatar vemundeldegard commented on May 20, 2024

In the parent component of the Article component.

const App = () => {
  const initialise = useAction(actions => actions.initialise);
  const articles = useStore(state => state.article.items);

  useEffect(() => {
    /*initialise();*/  //removed because of endless looop
    console.log(articles); //returns nothing
  });
  return (
    <Article />
  )
}

from easy-peasy.

frankzang avatar frankzang commented on May 20, 2024

That's it, you shoudn't call the initialise method inside the useEffect hook

from easy-peasy.

vemundeldegard avatar vemundeldegard commented on May 20, 2024

The Easy-Peasy example did that. Where should it be called then? Where ever I call it it goes to infinite loop..

from easy-peasy.

frankzang avatar frankzang commented on May 20, 2024

The useEffect triggers whenever your app mount, and when your state update, so when the articles are fetched, it will call the useEffect again... You can call the initialise method outside the useEffect, or simple add the second argument [] to useEffect, so it will run only on the initial mount.

Edit: Dont't call the method outside the useEffect, follow the example of my next comment.
Edit II: Also, the Easy-Peasy example is not directly using the state on the component it is calling "initialise", so it can call the initialise method without rerendering the component.

from easy-peasy.

vemundeldegard avatar vemundeldegard commented on May 20, 2024

Ahhhh!!! Solved it. Where can I find info about second argument []? Nothing in the React docs.

Also had to change the fetched action to this:

   fetched: (state, payload) => {
      return {
        ...state,
        items: payload
      };
    }

from easy-peasy.

frankzang avatar frankzang commented on May 20, 2024

You can find more about the [] argument in the docs, look for the footer note:
https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects

from easy-peasy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.