componentDidMount(){
var self = this;
//selt.props is undefined too from this point !!!! WHY
// this.props.addTodo();
window.onscroll = function(){
//self.setState({ type:'ADD_TODO' });
// debugger
self.props.addTodo('param')
}
}
function mapStateToProps (state) {
return {
todos: state
};
}
function mapDispatchToProps (dispatch, ownProps) {
return {
addTodo: function(text){
dispatch({type: 'ADD_TODO', text: text, id: ++_idSeq});
}
}
}
self.props.addTodo est pas une fonctionComment init change redux état de componentDidMount?
self.setState ({type: 'ADD_TODO'}); ne pas init réducteur! Pourquoi ?
Code réducteur:
function todoReducer (currentState, action) {
currentState = currentState || {}; // Initial State
// debugger
console.log('reducer!');
switch (action.type) {
case 'ADD_TODO':
debugger
let newTodo = {id:1};
return Object.assign({}, currentState, newTodo);
default:
return currentState; // TODO: Always return the state
}
}
// Create Store
var todoStore = createStore(todoReducer);
let unsubscribe = todoStore.subscribe(() => console.log(todoStore.getState()))
window.store = todoStore;
var TodoListContainer = connect(mapStateToProps, mapDispatchToProps)(TodoList);
pourriez-vous s'il vous plaît, clarifier comment vous projetez la structure est? et quel code est dans quel fichier? – JoseAPL