javascript - Action 不会触发 React + Redux 中的 reducer

标签 javascript ios reactjs react-native redux

我正在开发一个 react-redux 应用程序,由于某种原因,我调用的操作没有到达 reducer(我目前在其中只有一个日志语句)。我附上了我认为相关的代码,我们将不胜感激任何贡献。

在组件的函数内调用的 Action :

onSearchPressed() {
    console.log('search pressed');
    this.props.addToSaved();
}

Action /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

store/configure-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() 只是返回一个操作对象- 它不发送任何东西。

如果你想从一个组件中派发 Action ,你应该使用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 ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794236/

相关文章:

javascript - 如何列出backbone.js中的特定选项?

javascript - Internet Explorer 中模块的加载超时

ios - 核心数据应用组同步(带扩展)

JavaScript 没有被调用

javascript - 正则表达式允许输入字段中有 4 个数字(任意长度)

ios - UICollectionView UseLayoutToLayoutNavigationTransitions 与不同的数据源

ios - 在 iOS 7 中使用 superview 获取 UITableViewCell

javascript - 测试使用带有 Jest 和 enzyme 的样式组件创建的组件

javascript - 不变违规 : View config not found for name div

reactjs - Semantic UI React - Table - 使整行可选择并链接到某处