Search code examples
reactjsreduxreact-reduxreducers

React Redux Assign Arrow Function Warning in reducers


Can someone tell me why I'm getting this Warning in my React App?

src/reducers/posts.js Line 3:1: Assign arrow function to a variable before exporting as module default

import { FETCH_ALL, CREATE, UPDATE, DELETE } from '../constants/actionTypes';

export default (posts = [], action) => {
    switch (action.type) {
        case FETCH_ALL:
            return action.payload;
        case UPDATE:
            return posts.map((post) => post._id === action.payload._id ? action.payload : post);
        case CREATE:
            return [ ...posts, action.payload];
        case DELETE:
            return posts.filter((post) => post._id !== action.payload);
        default:
            return posts;
    }
}

Solution

  • add variable to your function:

    export default const variable = (posts = [], action) => {
        switch (action.type) {
            case FETCH_ALL:
                return action.payload;
            case UPDATE:
                return posts.map((post) => post._id === action.payload._id ? action.payload : post);
            case CREATE:
                return [ ...posts, action.payload];
            case DELETE:
                return posts.filter((post) => post._id !== action.payload);
            default:
                return posts;
        }
    }
    

    JavaScript have to know what are you exporting