Search code examples
node.jsreactjsmern

Error: Actions must be plain objects. Instead, the actual type was: 'string'


Error:

Actions must be plain objects. Instead, the actual type was: 'string'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions.

The below code is the client side where I dispatch the selected user id to actions.

  const friendHandle = (e) => {
        e.preventDefault()
        setSwitch(false)
        setFriend(!friend)
        dispatch(friendUser(id))//id is the id from params of selected users
        setFetchAgain(!fetchAgain)
    }

    useEffect(() => {
        if(currentUser){
            currentUser?.friends?.map(friends => {
                console.log(friends._id)
                console.log(currentProfile._id)
                if(friends._id===currentProfile._id){
                    return setFriend(true)
                }
            })
        }else{
            return setFriend(false)
        }
    },[currentUser,currentProfile])

below is actions.js for the above code

export const friendUser = (id) => async (dispatch) => {
    try {
     await api.friendUser(id)
        dispatch(id)
    } catch (error) {
        console.log(error)
    }
}

I am trying to pass the id of the selected user but I am getting an error. I am new to React so I am not able to understand.


Solution

  • remove the dispatch(id) from the action, might be some copy\paste or merge error