Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React Redux mapDispatchToProps未接收mapStateToProps_Redux_React Redux - Fatal编程技术网

React Redux mapDispatchToProps未接收mapStateToProps

React Redux mapDispatchToProps未接收mapStateToProps,redux,react-redux,Redux,React Redux,在我的mapstatetops函数中,我将idToken和accessToken设置为状态中存储的值。这是可行的,因为我已经能够从组件中引用这些值。在mapDispatchToProps中,我尝试在操作中使用这些道具作为参数。但是,ownProps是一个空对象。为什么它没有idToken和accessToken 容器: import { connect } from 'react-redux' import { toggleAddQuestionModal, fetchFriends } fro

在我的
mapstatetops
函数中,我将
idToken
accessToken
设置为状态中存储的值。这是可行的,因为我已经能够从组件中引用这些值。在
mapDispatchToProps
中,我尝试在操作中使用这些道具作为参数。但是,
ownProps
是一个空对象。为什么它没有
idToken
accessToken

容器:

import { connect } from 'react-redux'
import { toggleAddQuestionModal, fetchFriends } from '../actions'
import AddQuestionButtonComponent from '../components/AddQuestionButton'

const mapStateToProps = (state) => {
  auth = state.auth
  return {
    idToken: auth.token.idToken,
    accessToken: auth.profile.identities[0].accessToken,
  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    didPress: (idToken, accessToken) => {
      dispatch(toggleAddQuestionModal(true))
      dispatch(fetchFriends(ownProps.idToken, ownProps.accessToken))
    }
  }
}

AddQuestionButton = connect(
  mapStateToProps,
  mapDispatchToProps
)(AddQuestionButtonComponent)

export default AddQuestionButton
组成部分:

'use strict';

import React, {
  Text,
  View,
  TouchableHighlight,
  PropTypes,
} from 'react-native'

import styles from './styles'

const AddQuestionButton = ({ didPress, idToken, accessToken }) => (
  <TouchableHighlight style={styles.actionButton} onPress={didPress(idToken, accessToken)}>
    <Text style={styles.actionButtonText}>+</Text>
  </TouchableHighlight>
)
AddQuestionButton.propTypes = {
  idToken: PropTypes.string.isRequired,
  accessToken: PropTypes.string.isRequired,
  didPress: PropTypes.func.isRequired,
}

export default AddQuestionButton
“严格使用”;
进口反应{
文本,
看法
触控高光,
道具类型,
}从“反应本机”
从“./styles”导入样式
const AddQuestionButton=({didPress,idToken,accessToken})=>(
+
)
AddQuestionButton.propTypes={
idToken:PropTypes.string.isRequired,
accessToken:PropTypes.string.isRequired,
didPress:PropTypes.func.isRequired,
}
导出默认AddQuestion按钮
为什么我不能从
ownProps
访问
idToken
accessToken
?如果模式不正确,应如何访问
idToken
accessToken


谢谢

mapStateToProps
mapDispatchToProps
中,
ownProps
参数指组件通过属性接收的道具,例如:

方法本身有第三个参数,名为
mergeProps

[合并道具(stateProps、dispatchProps、ownProps):道具](功能): 如果指定,则传递MapStateTops()的结果, mapDispatchToProps()和父道具。你看到的朴素的物体 从它返回的数据将作为道具传递给包装的组件。你 可以指定此函数以根据选择状态片 道具,或将动作创建者绑定到道具中的特定变量。 如果省略它,Object.assign({},ownProps,stateProps,dispatchProps) 默认情况下使用

在合并道具中,您实际上可以将所有道具组合在一起,正如Dan Abramov对此的回答所示:

function mapStateToProps(state, ownProps) {
  return {
    isFollowing: state.postsFollowing[ownProps.id]
  };
}

function mergeProps(stateProps, dispatchProps, ownProps) {
  const { isFollowing } = stateProps;
  const { dispatch } = dispatchProps;
  const { id } = ownProps;

  const toggle = isFollowing ?
    unfollowPostActionCreator :
    followPostActionCreator;

  return {
    ...stateProps,
    ...ownProps,
    toggleFollow: () => dispatch(toggle(id)))
  };
}

ToggleFollowButton = connect({
  mapStateToProps,
  null, // passing null instead of mapDispatchToProps will return an object with the dispatch method
  mergeProps
})(ToggleFollowButton)