【问题标题】:connect function of reduxredux 的连接函数
【发布时间】:2021-12-27 19:59:04
【问题描述】:

我想从 redux 和 connect 函数开始。当我使用 useSelector 钩子时,一切都很好,所以我的 redux 状态有效。但是使用 connect 和 maptStateToProps 函数则不然。 number 变量未定义。

import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import { connect } from 'react-redux'
import { incrementNumber } from '../actions/exampleAction'

const Incrementor = ({ number, increment }) => {
  return (
    <View>
      <Text onPress={increment}>{number}</Text>
    </View>
  )
}

export default connect(mapStateToProps, mapDispatchToProps)(Incrementor)

const styles = StyleSheet.create({})

const mapStateToProps = (state) => {
  return {
    number: state.exampleReducer.number,
  }
};

const mapDispatchToProps = (dispatch) => {
  return {
    increment: () => dispatch(incrementNumber()),
  }
};

这是使用 react Hooks 的工作解决方案:

import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import { useSelector , useDispatch} from 'react-redux'
import { connect } from 'react-redux'
import { incrementNumber } from '../actions/exampleAction'

const Incrementor = () => {
  const dispatch = useDispatch();
  const number = useSelector(state => state.exampleReducer.number)

  return (
    <View>
      <Text onPress={() => {dispatch({type: "INCREMENT"})}}>{number}</Text>
    </View>
  )
}
const styles = StyleSheet.create({})

export default Incrementor

这是我的减速器功能:

import { INCREMENT } from "../actions/exampleAction";

let initialState = { number: 0 };

export default function reducer (state = initialState, action) {
  switch (action.type) {
    case INCREMENT:
      return {  ...state, number: state.number+1, };
    default:
      return state;
  }
}

这里是动作:

export const INCREMENT = "INCREMENT";

export const incrementNumber = () => {
  return {
    type: INCREMENT,
  }
};

在这里我结合了我的减速器:

import { combineReducers } from 'redux'

import exampleReducer from "./exampleReducer"

const reducers = combineReducers({
  exampleReducer,
});

export default reducers;

这是我的 App.js

import React from 'react';
import { StyleSheet, View } from 'react-native'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux';


import Incrementor from './src/components/Incrementor';
import reducer from "./src/reducers/index"

let store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())

export default function App() {
  return (
    <Provider store={store}>
      <View style={styles.container}>
        <Incrementor />
      </View>
    </Provider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

【问题讨论】:

  • connect 函数在这一点上只是为了与遗留类组件兼容而真正需要的,而且很有可能你永远不会在现代 React 项目中看到这些组件。我们正式建议尽可能使用挂钩。因此,尽管您看一下它还不错,但您可能不需要花太多时间在它上面。请参阅 Redux 样式指南:redux.js.org/style-guide/style-guide/…
  • 您能否更新您的问题以包含基于钩子的工作解决方案?我怀疑你没有从 redux 中选择正确的状态。
  • 是的,我加了。
  • 我看到您还编辑了您在mapStateToProps 中选择的内容,是否还有未定义的number 属性?
  • 是的,它仍然未定义。

标签: reactjs react-native redux react-redux react-hooks


【解决方案1】:

首先,检查你是否从actions中导入了incrementNumber(),然后你必须在定义完所有mapStateToProps & mapDispatchToProps后使用connect函数,你的代码会是这样的

const mapStateToProps = (state) => ({
   number: state
})

const mapDispatchToProps = (dispatch) => ({
 increment: () => dispatch(incrementNumber())
})

export default connect(mapStateToProps, mapDispatchToProps)(Incrementor)

【讨论】:

  • 除了内联一些代码之外,这与 OP 的代码有何显着不同?此外,您的 mapStateToProps 现在不正确,它没有返回对象。
  • 当我将连接函数放在 mapStateToProps 函数下时,我得到一个错误“对象作为反应子无效(找到:带键的对象 {reducer)”
  • 我没有复制你的代码你的错误我的代码中没有。
  • 实际上这个错误(我得到一个错误“对象作为反应子无效(发现:带有键的对象 {reducer)”)与您共享的代码无关,请检查您的减速器结构
  • 但是当我将“导出默认连接...”放在 mapStateToProps 函数下时,我得到了这个错误。当“导出默认连接...”在 mapStaetToProps 上方时,我没有收到此错误。
猜你喜欢
  • 2019-12-30
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
相关资源
最近更新 更多