【问题标题】:redux reducer not receiving an actionredux reducer 未收到操作
【发布时间】:2019-09-03 03:38:47
【问题描述】:

大家好, 今天是我第一次使用 redux,我按照 youtube 教程设置了我的样板设置,以使用带有 react-native 的 redux,显然,我遇到了一些问题,并且看不到我可能犯的错误,因为这个概念仍然对我来说有点模糊。

所以我创建了一个简化的、一个动作和一个类型作为起点。问题是在将状态连接到组件并在组件的构造函数中触发操作后,我从操作中获得了结果(使用控制台日志对其进行了测试),但状态没有改变。

这是我的代码:

商店

import {
    createStore,
    applyMiddleware
} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './src/reducers/rootReducer';

const initialState = {};

const middleware = [thunk];

const store = createStore(rootReducer, initialState, applyMiddleware(...middleware));

export default store;

根减速器

import { combineReducers } from 'redux';
import companyReducer from './companyReducer';

export default combineReducers({
    companies: companyReducer
});

公司减速器

import { FETCH_COMPANIES } from '../actions/types';

const initialState = {
    values: []
};

export default (state = initialState, action) => {
    switch(action.type) {
        case FETCH_COMPANIES: return {
            ...state,
            values: action.payload
        };
        default: return {
            test: 'testing'
        };
    }
}

公司行动

import {
    FETCH_COMPANIES
} from './types';

export const fetchCompanies = () => dispatch => 
    dispatch({
        type: FETCH_COMPANIES,
        payload: [{
                id: 1,
                text: "CHRONUS FRANCE .A."
            },
            {
                id: 2,
                text: "two"
            },
            {
                id: 3,
                text: "three"
            },
            {
                id: 4,
                text: "four"
            },
            {
                id: 5,
                text: "five"
            },
            {
                id: 6,
                text: "six"
            },
            {
                id: 7,
                text: "seven"
            },
            {
                id: 8,
                text: "eight"
            },
            {
                id: 9,
                text: "nine"
            },
            {
                id: 10,
                text: "ten"
            },
        ]
    });

类型

export const FETCH_COMPANIES = 'FETCH_COMPANIES';

组件

//imports
import {
  connect
} from 'react-redux';
import { fetchCompanies } from '../../actions/companyActions';

//constructor
class Welcome extends Component {
  constructor(props) {
    super(props);
    props.fetchCompanies();
    console.log(props);
  }
}

//map to props and connect
const mapStateToProps = (state) => ({
  companies: state.companies
});

export default connect(mapStateToProps, { fetchCompanies })(Welcome);

这是我在记录道具后在控制台上得到的:

{…}

公司:{…}

 test: "testing"
 <prototype>: Object { … }

fetchCompanies: "function () {\n return dispatch(actionCreator.apply(this, arguments));\n }"

navigation: Object { pop: "function () {\n var actionCreator = actionCreators[actionName];\n var action = actionCreator.apply(void 0, arguments);\n return navigation.dispatch(action);\ n }", popToTop: "function () {\n var actionCreator = actionCreators[actionName];\n var action = actionCreator.apply(void 0, arguments);\n return navigation.dispatch(action);\n }" , push: "function () {\n var actionCreator = actionCreators[actionName];\n var action = actionCreator.apply(void 0, arguments);\n return navigation.dispatch(action);\n }", ... }

screenProps:未定义

: 对象 { … } 305278fb-9c91-40bc-b0b1-9fa113a58b1f:93248:15

我希望这里有人能找到什么不起作用以及我错过了什么,并帮助我解决这个问题。 提前感谢您的时间和精力。

【问题讨论】:

  • the component 你没有导入 React。这是一个错误吗? import {Component} from 'react'
  • @chawkichalladia 请试试我的回答。
  • result from the action (tested it with a console log) 是什么意思?您能否将日志添加到您的问题中?
  • 我想你可能想像console.log(props.fetchCompanies())一样登录。
  • 好的,我会在你的回答下评论日志

标签: reactjs react-native redux react-redux


【解决方案1】:

您可能想通过在render 中记录道具来检查商店是否已更新。在您的构造函数中无法观察到任何道具的变化。

props.fetchCompanies() 正在调用一个返回函数的函数。该函数将被传递给 dispatch,然后被redux-thunk 拦截。

props.fetchCompanies() 只会调用外部函数,而不是内部函数。内部函数需要一个参数(dispatch),由 redux-thunk 提供。

const mapDispatchToProps = (dispatch) => {
   return {
      fetchCompanies: () => dispatch(fetchCompanies())
   }
}

export default connect(mapStateToProps, mapDispatchToProps)(Welcome);

注意

您不必如上所示手动添加调度,因为connect 会自动调度返回对象中的字段。

【讨论】:

  • 感谢您的帮助,但这并没有改变任何东西。
  • {...} 有效载荷:(10) [...] 0: Object { id: 1, text: "CHRONUS FRANCE .A." }​​1:对象{id:2,文本:“二”}​​2:对象{id:3,文本:“三”}​​3:对象{id:4,文本:“四”}​ ​4:对象{id:5,文本:“五”}​​5:对象{id:6,文本:“六”}​​6:对象{id:7,文本:“七”}​​​​ :对象{ id:8,文本:“八”}​​ 8:对象{id:9,文本:“九”}​​ 9:对象{id:10,文本:“十”}​​长度:10​​ : Array []​ type: "FETCH_COMPANIES"​ : Object { … } 4ac3dd6f-ba7d-437f-b45c-1e4b42a78146:93250:15
  • 这是日志。该操作有效,结果可用,只是未添加到状态
  • 该函数将被调用,但要调用内部函数,它必须被调度。
  • 我尝试了这篇文章中给出的建议,但到目前为止都没有奏效。我什至从它工作的教程中复制了该代码
【解决方案2】:

您是否检查过您的 actionTypes 是否正确导入?

编辑: 我检查了你的减速器默认情况,在情况 FETCH_COMPANIES 之前首先调用。不知道为什么要调用它,但是这样你的状态就会被测试属性覆盖。不再有 values 属性。然后在您的构造函数中调用 fetchCompanies。它为您的状态增加了价值。您的 mapStateToProps 中有 state.companies 但您所在的州没有这样的属性。试试

values: state.values 你会看到你的公司

【讨论】:

  • 是的,我通过在我的 switch 语句中返回默认情况下的类型来测试它
  • 没有真正的 redux 工作正常我只是不清楚它是如何工作的,所以我错过了几点。感谢您的帮助
【解决方案3】:

我想建议对操作进行细微的更改。

import {
    FETCH_COMPANIES
} from './types';

export const fetchCompanies = payload => ({type: FETCH_COMPANIES, payload})

像这样更新组件

    import {
      connect
    } from 'react-redux';
    import { bindActionCreators } from "redux";
    import { fetchCompanies } from '../../actions/companyActions';

    const payload = [{
            id: 1,
            text: "CHRONUS FRANCE .A."
        },
        {
            id: 2,
            text: "two"
        },
        {
            id: 3,
            text: "three"
        },
        {
            id: 4,
            text: "four"
        }]

    //constructor
    class Welcome extends Component {
      constructor(props) {
        super(props);
        props.fetchCompanies(payload);
        console.log(props);
      }
    }

    //map to props and connect
    const mapStateToProps = (state) => ({
      companies: state.companies
    });

   const mapDispatchToProps = dispatch => bindActionCreators({fetchCompanies}, dispatch)


    export default connect(mapStateToProps, mapDispatchToProps)(Welcome);

【讨论】:

  • 在我尝试fetchCompanies() 的操作中,它给了我和错误dispatch not a function。还尝试了fetchCompanies() =&gt; fetchCompanies(),在这两种情况下我最初的问题都没有改变
  • @chawkichalladia 我刚刚更新了答案,提出了一些小的改动
  • 所以你建议我做动作之外的动作然后传递结果?我是 redux 的新手,但这似乎与我读到的关于 redux 的内容相反。如果您不介意,您能否进一步解释一下这将如何工作。我现在有点困惑
  • 在公司行动中返回有效载荷。 export const fetchCompanies = () =&gt; dispatch =&gt; dispatch({ type: FETCH_COMPANIES, payload: [{,,, 更改为 => export const fetchCompanies = () =&gt; dispatch =&gt; dispatch( return { type: FETCH_COMPANIES, payload: [{
  • 我已经返回了一个有效载荷。为了测试 redux,我返回了一个静态有效负载
猜你喜欢
  • 2018-08-12
  • 2021-11-16
  • 2023-03-03
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
相关资源
最近更新 更多