Git Product home page Git Product logo

Comments (9)

Andriy-Kulak avatar Andriy-Kulak commented on May 18, 2024 16

I was facing with the same issue... below is a solution that worked for me on the mapStateToProps
This will render an empty state while data you're waiting for is still undefined. it will prevent things from breaking while your component is rendering. The if clause will depend on what you're trying to receive. I am waiting for an array of object to return while the component is rendering

function mapStateToProps(state) {

if (state.activePlayer.length > 0) {
return {
activePlayer: state.activePlayer
}
} else {
return {};
}
}

from redux-thunk.

Andriy-Kulak avatar Andriy-Kulak commented on May 18, 2024 3

@mesmerize86 length refers to an array data type so if response is not an array, this will throw an error. You can try as a quick fix.
function mapStateToProp(state){

if(state.tweets.tweets.response){
....

from redux-thunk.

JosephNam avatar JosephNam commented on May 18, 2024 1

Have you tried placing this.props.fetchActivePlayer into your constructor?

In ES6 classes the constructor takes the place of componentWillMount.

see here https://babeljs.io/blog/2015/06/07/react-on-es6-plus

from redux-thunk.

mesmerize86 avatar mesmerize86 commented on May 18, 2024

Hi @Andriy-Kulak

I got an error length is not defined when I tried in my response. Just wondering if its the right way to do it?
function mapStateToProp(state){
if(state.tweets.tweets.response.length > 0){
return {
tweets : state.tweets.tweets
}
}else{
return {};
}
}

from redux-thunk.

Anindya007 avatar Anindya007 commented on May 18, 2024

BTW @lawrenceleach according to ES6 spec I think you can't use export default twice in a single js file

from redux-thunk.

MohamedAbdelkarim avatar MohamedAbdelkarim commented on May 18, 2024

same here, but I can't move initialization to constructor because I can't use "async" in constructor.
what I did to get rid of annoying "of undefined" is to use "*ngIf =(data) " to make sure all data is fetched completely before showing the component.

from redux-thunk.

sstern6 avatar sstern6 commented on May 18, 2024

@gaearon any recommendations to cleanly handle this situation? ie how to handle in your react components when data has not been done fetching. This problem might be caused by many things, components expecting data, or waiting for some data from an endpoint to comeback before triggering another call etc.

Thanks

from redux-thunk.

ifeanyidavid avatar ifeanyidavid commented on May 18, 2024

A possible fix could be to write a check directly in your action creator dispatch the action only when the fetch actually returns something.

return axios.get(${ROOT_URL}/players/${slug}) .then(response => { if(response){ dispatch({ type: FETCH_ACTIVE_PLAYER, payload: response }) } }) .catch(err => { console.error("Failure: ", err); }); }; }

Worked for me.

from redux-thunk.

markerikson avatar markerikson commented on May 18, 2024

@KO527 : This is a bug tracker, not a support system. For usage questions, please use Stack Overflow or Reactiflux where there are a lot more people ready to help you out - you'll probably get a better answer faster. Thanks!

from redux-thunk.

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.