I'm use Redux with ReactNative,I'd like to create a store with reducer
And,I got error below, point to line 'switch (action.type)' in function toggleFavorite() in favoriteReducer.js. I found a similar topic but it didn't fix it...
undefined is not an object(evaluating 'action.type')
favoriteReducer.js :
const initialState = { favoriteQuotes: [] }
function toggleFavorite(state = initialState, action) {
let nextState
switch (action.type) {
case 'TOGGLE_FAVORITE':
const favoriteQuoteIndex = state.favoriteQuotes.findIndex(item => item.id === action.value.id)
if (favoriteQuoteIndex !== -1) {
// La citation est déjà dans les favoris, on la supprime de la liste
nextState = {
...state,
favoriteQuotes: state.favoriteQuotes.filter( (item, index) => index !== favoriteQuoteIndex)
}
}
else {
// La citation n'est pas dans les favoris, on l'ajoute à la liste
nextState = {
...state,
favoriteQuotes: [...state.favoriteQuotes, action.value]
}
}
return nextState || state
default:
return state
}
}
export default toggleFavorite()
configureStore.js :
import { createStore } from 'redux';
import toggleFavorite from './Reducers/favoriteReducer'
export default createStore(toggleFavorite)
Here is where I use "dispatch":
import React from 'react'
...
import { connect } from 'react-redux'
class QuoteDetail extends React.Component {
...
_toggleFavorite() {
const action = { type: "TOGGLE_FAVORITE", value: this.state.quote }
this.props.dispatch(action)
}
...
<Button title="Favoris" onPress={() => this._toggleFavorite()}/>
...
const mapStateToProps = (state) => {
return {
favoriteQuotes: state.favoriteQuotes
}
}
export default connect(mapStateToProps)(QuoteDetail)
Anyone have an idea ??
You don't need to call the function while exporting, which is causing the error in your case, change it to
export default toggleFavorite;
and it will work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.