我創造了這個組件:爲什麼我的狀態不正確地映射到Redux中的道具?
import React, { Component } from 'react';
import { connect } from 'react-redux';
export class Todos extends Component {
render() {
//todo remove
console.log('testing this.props.todos', this.props.todos);
//todo remove
debugger;
return (
<div>hello from todos</div>
);
}
}
const mapStateToProps = function (store) {
return {
todos: store.todos
}
}
export default connect(mapStateToProps)(Todos)
我使用的終極版,這是減速機:
const initialState = {
todos: [
{
name:'first todo'
}
]
}
const Todos = (state = initialState, action) => {
switch (action.type) {
case "ADD_TODO":
var newState = Object.assign({}, state, {todos: [...state.todos, action.data]});
return newState;
default:
//todo remove
debugger;
return state;
}
}
export default Todos;
出於某種原因,this.props.todos
是不確定的,我的狀態不會被映射到我的道具正確。我怎樣才能將Redux商店連接到我的組件?
你能在mapStateToProps日誌存儲和看看你得到了什麼? –
現在看到下面都很好 –