javascript – Action不会在React Redux中触发reducer
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Action不会在React Redux中触发reducer,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2221字,纯文字阅读大概需要4分钟。
内容图文
我正在研究react-redux应用程序,由于某种原因,我调用的动作没有到达reducer(我目前只有一个日志语句).我附上了我认为相关的代码,我们将非常感谢任何贡献.
在组件中的函数内调用的操作:
onSearchPressed() {
console.log('search pressed');
this.props.addToSaved();
}
动作/ index.js:
var actions = exports = module.exports
exports.ADD_SAVED = "ADD_SAVED";
exports.addToSaved = function addToSaved() {
console.log('got to ADD_SAVED step 2');
return {
type: actions.ADD_SAVED
}
}
减速器/ items.js:
const {
ADD_SAVED
} = require('../actions/index')
const initialState = {
savedList: []
}
module.exports = function items(state = initialState, action) {
let list
switch (action.type) {
case ADD_SAVED:
console.log('GOT to Step 3');
return state;
default:
console.log('got to default');
return state;
}
}
减速器/ index.js:
const { combineReducers } = require('redux')
const items = require('./items')
const rootReducer = combineReducers({
items: items
})
module.exports = rootReducer
存储/配置 – store.js:
import { createStore } from 'redux'
import rootReducer from '../reducers'
let store = createStore(rootReducer)
编辑:onSearchPressed的整个组件:
class MainView extends Component {
onSearchPressed() {
this.props.addToSaved();
}
render() {
console.log('MainView clicked');
var property = this.props.property;
return (
<View style={styles.container}>
<Image style={styles.image}
source={{uri: property.img_url}} />
<Text style={styles.description}>{property.summary}</Text>
<TouchableHighlight style = {styles.button}
onPress={this.onSearchPressed.bind(this)}
underlayColor='#99d9f4'>
<Text style = {styles.buttonText}>Save</Text>
</TouchableHighlight>
</View>
);
}
}
module.exports = MainView;
解决方法:
正如Rick Jolly在你的问题的评论中提到的那样,你的onSearchPressed()函数实际上并没有调度那个动作,因为addToSaved()只是返回一个动作对象 – 它不会发送任何东西.
如果要从组件调度操作,则应使用react-redux将组件连接到redux.例如:
const { connect } = require('react-redux')
class MainView extends Component {
onSearchPressed() {
this.props.dispatchAddToSaved();
}
render() {...}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatchAddToSaved: () => dispatch(addToSaved())
}
}
module.exports = connect(null, mapDispatchToProps)(MainView)
有关更多信息,请参见‘Usage With React’ section of the Redux docs.
内容总结
以上是互联网集市为您收集整理的javascript – Action不会在React Redux中触发reducer全部内容,希望文章能够帮你解决javascript – Action不会在React Redux中触发reducer所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。