【发布时间】:2016-08-16 02:36:51
【问题描述】:
我正在开发一个 react-redux 应用程序,由于某种原因,我调用的操作没有到达 reducer(我目前只有一个 log 语句)。我附上了我认为相关的代码,任何贡献都将受到高度赞赏。
在组件的函数内调用的动作:
onSearchPressed() {
console.log('search pressed');
this.props.addToSaved();
}
actions/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
}
}
reducers/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;
}
}
reducers/index.js:
const { combineReducers } = require('redux')
const items = require('./items')
const rootReducer = combineReducers({
items: items
})
module.exports = rootReducer
存储/配置存储.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;
【问题讨论】:
-
检查 onSearchPressed() 上的 console.log(this.props) 并确保它不为空
-
@QoP console.log(this.props) 已正确填充。
-
这很奇怪!尝试将exports.addToSaved = function addToSaved(){}更改为exports.addToSaved = function (){}
-
@QoP 仍然只到达 action log 语句,而不是 reducer log 语句:\
-
您没有发送您的操作。
this.props.addToSaved();应该是this.props.dispatch(addToSaved());
标签: javascript ios reactjs react-native redux