When I try to go to another user's profile page I get an endless warning in console and I don't get the page and there is nothing else to do on the page so I have to close it and open new tap again.
profileAction.js
import { GLOBALTYPES } from "./globalTypes";
import { getDataAPI } from "../../utils/fetchData";
export const PROFILE_TYPES = {
LOADING: 'LOADING',
GET_USER: 'GET_USER'
}
export const getProfileUsers = ({users, id, auth}) => async (dispatch) => {
if(users.every(user => user._id !== id)){
try {
dispatch({type: PROFILE_TYPES.LOADING, payload: true})
const res = await getDataAPI(`/user/${id}`, auth.token)
console.log(res)
} catch (err) {
dispatch({
type: GLOBALTYPES.ALERT,
payload: {error: err.response.data.msg}
})
}
}
}
Info.jsx
import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import Avatar from '../Avatar';
import { getProfileUsers } from '../../redux/actions/profileAction';
const Info = () => {
const { id } = useParams()
const { auth, profile } = useSelector(state => state);
const dispatch = useDispatch()
const [userData, setUserData] = useState([])
useEffect(() => {
if(id === auth.user._id){
setUserData(auth.user)
// console.log(userData)
}else{
dispatch(getProfileUsers({users: profile.users, id, auth}))
}
}, [id, auth.user, profile.users])
const followed = userData.followers || []
const follow = userData.following || []
return (
<div className='info'>
{userData ?
<div className="info_container" key={userData._id}>
<Avatar src={userData.avatar} size="supper-avatar"/>
<div className="info_content">
<div className="info_content_title">
<h2>{userData.username}</h2>
<button className="btn btn-outline-info">
Edit Profile
</button>
</div>
<div className="follow_btn">
<span className="mr-4">
{followed.length} Followers
</span>
<span className="ml-4">
{follow.length} Following
</span>
</div>
<h6>{userData.fullname}</h6>
<h6>{userData.email}</h6>
</div>
</div> : null
}
</div>
)
}
export default Info
By the way, I always get this warning before, but the pages were working fine and the warning was not endless

This can happen when you are selecting "too much state", in this case you are subscribing to all state. In other words, any time any state is updated, regardless if it is related to what is destructured/used in
Info, theInfocomponent will be triggered to rerender.The solution is to narrow the scope of what you are selecting.
This will trigger
Infoto rerender when any of theauthorprofilestate value changes, but not other state.Based on the code that
Infoactually references, the scope can be narrowed further still.Now this should only trigger a component rerender when specifically the
userorusersstates update.The render looping issues are likely related this
useEffecthook call. It is a bit of a React anti-pattern to duplicate passed/selected values/"derived state" into local state. TheuserDatavalue should be computed directly from the currentidanduservalues. Just about any time you find that you have coded auseState/useEffectcoupling you should really use theuseMemohook to compute (and memoize) a stable reference value.This could be further improved by returning the computed user data directly in the
useSelectorhook.Update the
getProfileUsersaction to consume only anidargument and use the Thunk'sgetStatefunction to access the store value. This removes the other external dependencies.