【问题标题】:rudux with react-native dont work -- (0, _combineReducers.default) is not a function带有 react-native 的 redux 不起作用 - (0, _ combineReducers.default) 不是函数
【发布时间】:2021-12-16 21:58:19
【问题描述】:

我在 react 应用程序中所做的完全一样,但在 react 中工作,在 react-native 中不起作用

app.js 文件

import React from 'react';
import { Provider } from 'react-redux';
import { store } from './redux/reducers/combineReducers'

var Stack = createNativeStackNavigator()
export default function App() {
  return (<Provider store={store()}>  // if i write store={store} error: undefined is not an object (evaulating 'store.getStare')
      </Provider>
  );
}

combinedReducers.js

import { key} from "./reducerKey";

    import { combineReducers } from 'redux'
    
    export var reducers = combineReducers({
        storeKey: key,
    })

reducerKey 文件

export var key = (state = 0, action) => {
    if (action.type === 'changeKey') {
        return action.playload
    }
    else {
        return state
    }
}

【问题讨论】:

  • 因为你试图破坏combinedReducers.js中不存在的{store}
  • 您正在导出“reducers”并导入“store”。如果不是这样,import { reducers } from './redux/reducers/combineReducers'。另外,我认为你应该这样做,import { reducers } from './redux/reducers/combineReducers'; const store = reducers(); &lt;Provider store = { store }&gt; &lt;App /&gt; &lt;/Provider&gt;

标签: javascript react-native redux react-redux jsx


【解决方案1】:

您没有创建store,而是尝试导出reducer

combinedReducers.js

import {createStore, combineReducers} from 'redux';

const rootReducer = combineReducers({
   storeKey: key,
})

const store = createStore(rootReducer)

export {store}

现在在App.js,您可以使用store(无需调用它):

import {store} from 'path/to/store';

// rest of the codes ...
<Provider store={store}>
// rest of the codes ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-19
    • 2019-04-24
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2016-07-01
    相关资源
    最近更新 更多