Nella mia funzione mapStateToProps
ho impostato idToken
e accessToken
in valori memorizzati nello stato. Funziona, dato che sono stato in grado di fare riferimento a questi valori dal componente. Nel mapDispatchToProps
provo ad usare questi oggetti di scena come argomenti nella mia azione. Tuttavia, ownProps
è un oggetto vuoto. Perché non ha idToken
e accessToken
?React-Redux mapDispatchToProps non riceve mapStateToProps
contenitore:
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
componente:
'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
Perché non è possibile accedere idToken
e accessToken
da ownProps
? Se ciò avviene in uno schema errato, come si può accedere a idToken
e accessToken
?
Grazie!
Ah ho visto 'mergeProps' nel doc, ma non sapeva come usarlo. Grazie mille! – Cole
fantastico! Proprio quello che stavo cercando. Potrei aggiungere che il mergeProps dovrebbe anche restituire stateProps quindi dovrebbe apparire come 'return {... ownProps, ... stateProps, toggleFollow:() => dispatch (toggle (id))};' – Cristian
@Cristian - nice catturare. Fisso. –