I
I
Im p3l2021-02-07 16:04:16
React
Im p3l, 2021-02-07 16:04:16

How to fix TypeError: Cannot read property 'isFollowed' of undefined. React?

When I click on the button, I want to change the state of the button and I get an error: TypeError: Cannot read property 'isFollowed' of undefined in usersReducer

const initialState = {
    users: [
        {
            id: 2,
            fullName: 'Arnold',
            status: 'Follow me',
            location: {
                city: 'Moscow',
                country: 'Russia'
            },
            isFollowed: false
        },
        {
            id: 3,
            fullName: 'Alex Terrible',
            status: 'New song coming soon',
            location: {
                city: 'Yekaterinburg',
                country: 'Russia'
            },
            isFollowed: false
        },
        {
            id: 4,
            fullName: 'Phil Bozeman',
            status: 'New album tomorrow',
            location: {
                city: 'Knoxville',
                country: 'USA'
            },
            isFollowed: false
        },
        {
            id: 5,
            fullName: 'Andrew Martin',
            status: 'Hi guys',
            location: {
                city: 'Toronto',
                country: 'Canada'
            },
            isFollowed: false
        }
    ]
};

const usersReducer = (state = initialState, action) => {
    switch (action.type) {
        case TOGGLE_FOLLOW: {
            return {
                ...state,
                users: state.users.map(user => {
                    if (user.id === action.id) {
                        return {
                            ...user,
                            isFollowed: !this.isFollowed
                        };
                    }
                    return user;
                })
            };
        }
        default: {
            return state;
        }
    }
};

const toggleFollow = (id) => ({
    type: TOGGLE_FOLLOW,
    id
});

const Users = (props) => {
    const user = props.users.map(user => {
        return <div key={ user.id } className={ styles.box }>
            <figure>
                <img src='' alt="" className={ styles.bg } />
            </figure>
            <div className={ styles.meta }>
                <img src={ user.avatar } className={ styles.avatar } alt='' />
                <div className={ styles.name }>
                    <a href="" className={ styles.link }>
                        { user.fullName }
                    </a>
                    <span className={ styles.location }>
                        { user.location.city }, { user.location.country }
                    </span>
                </div>
                <ul className={ styles.info }>
                    <li>
                        { user.status }
                    </li>
                </ul>
                <button onClick={ () => {
                    props.toggleFollow(user.id);
                } } className={ styles.button }>
                    { user.isFollowed ? 'Unfollow' : 'Follow' }
                </button>
            </div>
        </div>;
    });

    return (
        <div>
            { user }
        </div>
    );
};

const mapStateToProps = (state) => {
    return {
        users: state.usersPage.users
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        toggleFollow: (id) => {
            dispatch(toggleFollowAC(id));
        }
    };
};

connect(mapStateToProps, mapDispatchToProps)(Users);

const reducers = combineReducers({
    usersPage: usersReducer
})

const store = createStore(reducers);

const App = () => {
    return ( < UsersContainer /> );
};

ReactDOM.render(
        <Provider store={ store }>
            <App />
        </Provider>,
    document.getElementById('root')
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-02-07
@LoranDeMarcus

return {
    ...user,
    isFollowed: !this.isFollowed
};

I would like to know: where did you get the idea that user and this are the same thing?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question