Comments (9)
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.
@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.
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.
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.
BTW @lawrenceleach according to ES6 spec I think you can't use export default twice in a single js file
from redux-thunk.
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.
@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.
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.
@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)
- Why `redux` module extension is not part of index.d.ts file in package? HOT 1
- Passing `dispatch` instead of calling it.
- Empty payload in PENDING state HOT 1
- Why not next? HOT 1
- types wrong by infer in TS HOT 1
- No type hints when using dispatch HOT 4
- import 'redux-thunk/extend-redux' doesn't work HOT 12
- When running server getting expected errors HOT 2
- ThunkAction is not assignable to parameter of type 'AnyAction'. HOT 46
- Current implemention doesn't allow Jest or any other mock tool to spy on dispatch or getState HOT 2
- TS do not show the error if payload is Partial<> HOT 1
- Can I abort redux thunk dispatches? Without Redux toolkit or aborting the fetch request itself. HOT 1
- Allow wrapped thunk actions for extensibility HOT 2
- When type: "module" is used in root package.json, redux-thunk cannot be applied without crashing HOT 8
- Warning in React-thunk while installing HOT 1
- Query About Redux Thunk HOT 1
- `thunk` as a named export? HOT 3
- redux thunk cannot be use as middleware using Typescript 5.0 or above HOT 1
- during integrating thunk into react-redux project, getting middleware is not a function error. HOT 1
- No default export for redux-thunk HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-thunk.