Bonjour,
Voila je rencontre un petit problème avec mon code.
J'ai créé une méthode pour récupérer les utilisateurs mais mon props est vide ?
Point d'entrée (Create.js)
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from "redux-thunk"
import reducers from '../../reducers';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
import {connect} from "react-redux"
import {bindActionCreators} from "redux"
import {readAllUsers} from "../../actions/users";
class Create extends React.Component {
componentWillMount() {
console.log(this.props);
}
render() {
return (
<h1>Test</h1>
)
}
}
const mapStateToProps = (state) => {
return {
users: state.users
}
};
function mapDispatchToProps (dispatch) {
return bindActionCreators({readAllUsers},dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Create)
if (document.getElementById('hdapp')) {
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())}>
<Create />
</Provider>
, document.getElementById('hdapp'));
}
Reducers (index.js)
import { combineReducers } from 'redux';
import ReducerUsers from "./reducer-users";
const rootReducer = combineReducers({
users: ReducerUsers,
});
export default rootReducer;
Reducer Users (reducer-users.js)
import {AT_USERS} from "../actions/users/action-types";
export default function reducerUsers (state=[],action){
switch(action.type){
case AT_USERS.READ_ALL :
return action.payload;
}
return state
}
Action type users
export const AT_USERS = {
READ_ALL : "READ_ALL_USER",
};
Action users index
import axios from "axios"
import {END_POINT, API_KEY} from "../../utils";
import {AT_USERS} from "./action-types";
export function readAllUsers(){
return function(dispatch){
axios.get(`${END_POINT}/api/users?api_token=${API_KEY}`).then( (response) => {
dispatch({type:AT_USERS.READ_ALL,payload:response.data})
});
}
}
J'aimerais savoir pourquoi mon props est vide, je pense avoir écrit le bon code pour que ça fonctionne