Skip to content

How to use with redux-thunk ? #125

Description

@nilaybrahmbhatt

hi,
my action is like this

import axios from 'axios';
export const FETCH_USER = 'FETCH_USER';

export function fetchUsers(){
  return function(dispatch) {
     axios.get(`http://localhost:3000/api/endpoint`)
    .then(response => {
      //return response;
      dispatch( { type: FETCH_USER, payload: response} );
    });
  }
}

please tell me how to connect this using asyncConnect ?

I tried this.

import React, { Component, PropTypes } from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { fetchUsers } from '../store/reducers/user_action';
import { asyncConnect } from 'redux-connect';

const $asyncConnect = asyncConnect([{
key: 'user',
promise: ({ store, helpers }) => {
const { user } = store.getState().user;
if (user) {
return Promise.resolve({data:user});
}
return store.dispatch(fetchUsers());
},
}])
class Test extends Component {
componentWillMount() {
this.props.fetchUsers();
}
render() {
var { user } = this.props;
return (


Test


{
user?user.value:'loading...'
}

);
}
}
function mapStateToProps(state) {
console.log(state);
return {
user:state.user.user
};
}
const $connect = connect(mapStateToProps, { fetchUsers });
export default compose($asyncConnect, $connect)(Test);

but not done.
please tell me how to do this ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions