我正在尝试使用redux来保存信息-我以前已经开始使用它,但是我正在尝试保存为其他属性名称。
我主要是从本教程开始的:
https://decembersoft.com/posts/redux-hero-part-1-a-hero-is-born-a-fun-introduction-to-redux-js/
我的操作文件:
const Actions = {
LEVEL_UP: 'LEVEL_UP',
LEVEL_DOWN: 'LEVEL_DOWN',
SAVE_COLOR: 'SAVE_COLOR'
};
// I also have action creators for levelUp and levelDown
const saveColor = (color) => ({
type: Actions.SAVE_COLOR,
payload: color
});
export default {levelUp, levelDown};
export {saveColor};
在我的减速器中:
import general, {saveColor} from './reducers/general.js'
export default (history) => combineReducers({
auth: auth,
router: connectRouter(history),
general: general,
saveColor: saveColor,
})
在我的组件文件中:
import general from '../actions/general'
import saveColor from '../actions/general'
const mapStateToProps = (state) => {
return {
general: state.general,
saveColor: state.saveColor
};
};
function mapDispatchToProps(dispatch) {
return bindActionCreators(saveColor, dispatch);
}
const Swatch = connect(mapStateToProps, mapDispatchToProps)(Swatch1);
但是,当我尝试在Swatch1组件中使用此命令时,我可以确认它到了,没有运气。
render() {
return (
<ColorExtractor
src={this.props.merchant.merchant.coverPhoto}
getColors={colors => this.lightestColor(colors)}
/>
);
}
this.lightestColor
具有以下命令:
this.props.saveColor(500);
我绝对可以确认它在此saveColor()命令之前就正确了-但该命令本身似乎不起作用。
如何可靠地使saveColor起作用?我早些时候使用通用时就可以使用。
编辑:来自reducers的general.js:
import Actions from '../actions/general.js'
/*export default (state = 1, action) => {
switch (action.type) {
case 'LEVEL_UP':
return state + 1;
case 'LEVEL_DOWN':
return state - 1;
case 'SAVE_COLOR':
return {color: action.payload}
}
return state;
};*/
export default (state = 1, action) => {
switch (action.type) {
case 'SAVE_COLOR':
return {color: action.payload}
}
return state;
};
//export {color};
我已经注释掉了原始的默认导出,以及最近通过将颜色设置为默认值而将其更改为的内容。