Pass Redux selector value to react state without rerendering in useEffect - reactjs

I want to build simple scroll pagination, so I update currentState with oldState and add new data to old state, meanwhile this new data comes from redux selector,
const Posts = () => {
const [page, setpage] = useState(1);
const [currentPosts, setCurrentPosts]: any = useState([]);
const dispatch = useDispatch();
const postList = useSelector((state: any) => state.posts);
const { loading = true, error, posts = [] }: IPostList = postList;
const handleClick = (id: number) => {
dispatch(listComments(id));
};
const handleScroll = (event: any) => {
const { scrollTop, clientHeight, scrollHeight } = event.currentTarget;
if (scrollHeight - scrollTop === clientHeight) {
setpage((prev) => prev + 1);
}
};
useEffect(() => {
dispatch(listPosts(page));
setCurrentPosts((prev: any) => [...prev, ...posts]);
}, [dispatch, page, posts]);
return (
<div onScroll={handleScroll} className="posts">
{posts.map((post, index) => (
<PostCard
onClick={() => handleClick(post.id)}
data={post}
key={index}
/>
))}
</div>
);
};
export default Posts;
I understand it causes an infinite loop because whenever posts are updated, listPosts are called, then it's updated again and again.
Can you provide me with the right solution and explanation on how to update currentPosts state while spreading the old state and new state which comes from the redux selector?
I think the code says more than my explanation.

Can you run this as two different effects? I believe this should eliminate the infinite loop.
useEffect(() => {
dispatch(listPosts(page));
}, [dispatch, page]);
useEffect(() => {
setCurrentPosts((prev: any) => [...prev, ...posts]);
}, [posts]);

Related

undefined children on react parent. However when console.log state is ldefined

I'm trying to load a component using useEffect hook and axios. However, when loading the page the components are not render, but when inspecting the parent component, his children are undefined (I'm very new to react)
Component
export const BonusRows = () => {
const [state, setState] = useState([]);
const [order, setOrder] = useState("asc");
const bonusRequest = () => {
axios
.get(`http://localhost:4000/api/v1/process/creator/gutierad5`)
.then((res) => {
const bonus = res.data;
const processes = bonus.processes;
setState(processes);
console.log(processes);
});
};
useEffect(() => {
bonusRequest();
}, []);
return (
<ProcessTable
funcionality={() => {
sortByDate(state, setState, order, setOrder);
}}
>
<Accordion allowToggle allowMultiple>
{state.map((element, index) => {
<AccordionItemSlot
key={index}
proccessID={element.id}
title={element.name}
targeruser='gutierad5'
createDate={FormatDateInYYMMDD(element.createdAt)}
status={element.status}
creator={element.creator}
links={element.links}
amount={element.processBonus.amount}
updatedAt={element.updatedAt}
password={element.processBonus.password}
franchise={element.processBonus.franchise}
/>;
})}
</Accordion>
</ProcessTable>
);
};
I don't know what I'm doing wrong. My best guess is that the state is not present when the component is loaded, so thet's the reasong is undefined. However when console log the state, the data is there.

Uncaught TypeError: can't access property "map", sizes is undefined

I am trying to map the prop sizes, that I'm saving in a state when the item has been loaded from the api, but I keep getting this error:
"Uncaught TypeError: can't access property "map", sizes is undefined"
const ItemDetailContainer = () => {
const { id } = useParams()
const [item, setItem] = useState({})
const [related, setRelated] = useState([])
const [sizes, setSizes] = useState([])
const [loading, setLoading] = useState(true)
const getRelated = () => {
const relatedItems = (productList.filter(product => product.category === item.category))
const clearRelated = relatedItems.filter(product => product.id !== item.id)
setRelated(clearRelated)
}
const setsizing = () => {
setSizes(item.sizes)
}
useEffect(() => {
customFetch(3000, productList.find(product => product.id == id))
.then(res => setItem(res))
.catch(error => console.log(error))
.finally(setLoading(false))
}, [])
//This is a solution to get the sizes and related items to load after the item has been loaded
useEffect(() => {
getRelated()
setsizing()
console.log(sizes);
}, [item])
return (
<>
loading ? <Spinner />
:
<ItemDetail
name={item.name}
price={item.price}
img={item.img}
stock={item.stock}
category={item.category}
description={item.description}
sizes={sizes}
related={related}
/>
</>
)
}
There are few mistakes in usage of React hook.
1. You should not access state variable as soon as you set the state. Because value is not reliable at all.
setsizing()
console.log(sizes); // This sizes is not updated value in Reactjs.
2. You should provide correct dependencies in your hooks and can remove unnecessary functions.
In the following code, you need to add productList at least.
useEffect(() => {
customFetch(3000, productList.find(product => product.id == id))
.then(res => setItem(res))
.catch(error => console.log(error))
.finally(setLoading(false))
}, [])
3. You can write one line code to get the related list.
Here is the updated code snippet you can refer to.
const ItemDetailContainer = () => {
const { id } = useParams()
const [item, setItem] = useState({})
const [related, setRelated] = useState([])
const [sizes, setSizes] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
customFetch(3000, productList.find(product => product.id == id))
.then(res => setItem(res))
.catch(error => console.log(error))
.finally(setLoading(false))
}, [productList])
//This is a solution to get the sizes and related items to load after the item has been loaded
useEffect(() => {
if (item && productList) {
const related = (productList.filter(product => product.category === item.category && product.id !== item.id))
setRelated(related);
setSizes(item.sizes);
}
}, [item, productList]);
return (
<>
loading ? <Spinner />
:
(item? <ItemDetail
name={item.name}
price={item.price}
img={item.img}
stock={item.stock}
category={item.category}
description={item.description}
sizes={sizes}
related={related}
/> : <div>Item does not exist!</div>)
</>
)
}

UseEffect is not called

I have a question about useEffect. My useEffect is not fetching the data the first time, I have to switch route for it to have the data I needed
const Comments = ({ ...rest }) => {
const theme = useTheme();
const classes = useStyles({ theme });
const [users, setUsers] = useState([]);
const { push } = useHistory();
const { token, loading } = useContext(AuthContext)
const dispatch = useDispatch();
const allUsers = useSelector(state => state.allUsers);
const comments = useSelector(state => state.listCommentsByBookId);
const listBooks = useSelector((state) => state.userListBooks);
const isFetching = useSelector((state) => state.isFetching);
const [stateReady, setReadyForRender] = useState(false)
const redirectTo = ( rowData ) => {
push({
pathname: ROUTE.USERS_DETAILS,
user: rowData
});
}
const options = {
filterType: 'checkbox',
selectableRowsHeader: false,
selectableRowsHideCheckboxes: false,
selectableRowsOnClick: false,
onRowClick: redirectTo,
};
const getAllComments = async () => {
var allusersId = [];
//get all ids
await allUsers.map((user) => {
allusersId.push(user.uid);
})
//get all books from users
await allusersId.map(async (id) => {
await dispatch(getUserListBooks(apiURL + `api/bdd/userListBooks/${id}`, token))
})
var listArray = [];
//filter the array and delete empty rows
listArray.push(listBooks);
var newArray = listArray.filter(e => e);
//map every user and stock the list of books in string
await newArray.forEach(async (book)=> {
await book.map(async (book) => {
await dispatch(getCommentsByBookId(apiURL + `api/bdd/ratingByBook/${book.id}`, token));
})
})
setReadyForRender(true)
}
useEffect(() => {
console.log('is fetching', isFetching)
if(comments.length === 0) {
getAllComments();
}
}, [stateReady])
console.log('COM', comments);
return (
<div>
{stateReady &&
<Card>
<Box className={classes.tableContainer} sx={{ minWidth: 1050 }}>
<MUIDataTable
data={comments}
columns={columns}
options={options}
/>
</Box>
</Card>}
</div>
);
};
Why? It might be related to async await but I'm stuck here.
If you want to fetch these informations on the first render, you'll have to pass an empty array as the second parameter of your useEffect.
The reason your useEffect is not called is because stateReady does not change during the course of your current code.
See this link, particularly the note section, it explains way better than me how the empty array as second parameter works.
Can you replace the useEffect section to the below code:
useEffect(() => {
(async () => {
console.log('is fetching', isFetching)
if(comments.length === 0) {
getAllComments();
}
})()
}, [stateReady])
You can read more about this in this link
You can use eslint to show errors when coding with hooks. In this case if you want useEffect to handle stateReady, please provide it in the function getAllComments() => getAllComments(stateReady) and when you call this function in useEffect with [stateReady] as dependencies, it'll work.
You should remove stateReady from your dependency array in the useEffect hook. Adding variables in the dependency array means that the use Effect hooks fires only when one of the dependencies changes. Here's how to use useEffect as lifecycle methods https://reactjs.org/docs/hooks-effect.html
useEffect(() => {
console.log('is fetching', isFetching)
if(comments.length === 0) {
getAllComments();
}
});

Refresh tag info

have some problem, do little pokedex, have after chose the cound of cards on the page i need to reload a container with cards, can u help me?
To get selected item use onSelect,
handleSelect = (e) => {
this.setState({value:e})
}
<DropdownButton
variant="danger"
alignRight
id="dropdown-menu-align-right"
onSelect={this.handleSelect}>
and get it to link in component Pokemon list
<div className="col">
<PokemonList pages={this.value} />
</div>
PokemonList working like this
function PokemonList({ pages }) {
const [pokemonList, setPokemonList] = useState([]);
const [currPage, setCurrPage] = useState(
`https://pokeapi.co/api/v2/pokemon?offset=0&limit=${pages}`
);
const [nextPage, setNextPage] = useState();
const [prevPage, setPrevPage] = useState();
const [pageNum, setPageNum] = useState(0);
useEffect(() => {
let cancel;
axios
.get(currPage, {
cancelToken: new axios.CancelToken((c) => (cancel = c)),
})
.then((res) => {
setPokemonList(res.data.results);
setPrevPage(res.data.previous);
setNextPage(res.data.next);
})
.catch((error) => {
console.log(error);
});
return () => {
cancel();
};
}, [currPage, pageNum]);
i don't know but after select item at deop down, nothing changes, can u please help me
Find the problem, i tried to change only value, but i should to change the link, so answer was:
const handleChange = (e) => {
setCurrPage(`https://pokeapi.co/api/v2/pokemon?offset=${pageNum}&limit=${e}`);
};
The problem is that you try to store props inside state and with useEffect listen on state change. Because of this React can't properly update components. The currPage state doesn't change when pages change. You should avoid this because it's anti-pattern.
Working short example:
const Component = ({pages}) => {
const [pokemonList, setPokemonList] = useState([]);
useEffect( () => {
fetch("/api/pokemons/" + pages )
.then( res => res.json() )
.then( data => setPokemonList(data.pokemonList ))
.catch( err => console.log("handle errors") )
})
return <div>{ pokemonList.map( pokemon => <div>{ pokemon.name }</div>) }</div>
}

React Hooks: useContext returning undefined in function

I have a small localization provider library that makes calls to a server to get translations based on the user's selected language. I've omitted a few helper functions for brevity. Here is the gist of it:
const LocaleContext = createContext();
const LocaleProvider = ({ children }) => {
const [state, setState] = useState(defaultValues);
if (state.phrases === null) {
changeLanguage(defaultLanguage);
}
return (
<LocaleContext.Provider value={[state, setState]}>
{children}
</LocaleContext.Provider>
);
};
const changeLanguage = (lang) => {
const [state, setState] = useContext(LocaleContext);
fetch(localizationEndpointForThatLanguage)
.then(response => response.json())
.then(data => {
setState({ ...state, phrases: data });
};
const translateString = (string) => {
const [state] = useContext(LocaleContext);
// if the string argument is found in state, return the translated value;
}
export { LocaleContext, LocaleProvider, changeLanguage, translateString };
Here is the component that uses it:
const Home = () => {
const [state, setState] = useContext(SiteContext);
useEffect(() => {
state.fetchContent(opts, state, setState);
}, []);
return (
<div>
{state.data && state.data.map((datum) => <div>{datum.title}</div>)}
<div onClick={() => changeLanguage('pl_PL')}>Change to Polish</div>
<div>{changeLanguage('Translate me')}</div>
</div>
);
};
The code gives me the error "TypeError: Cannot read property '0' of undefined" when trying to destructure the LocaleContext in the changeLanguage function. It works fine for the translateString function. It works if I manually pass state into the changeLanguage function.
const Home = () => {
const [state, setState] = useContext(SiteContext);
const [locale, setLocale] = useContext(LocaleContext);
useEffect(() => {
state.fetchContent(opts, state, setState);
}, []);
return (
<div>
{state.data && state.data.map((datum) => <div>{datum.title}</div>)}
<div onClick={() => changeLanguage('pl_PL')}>Change to Polish</div>
<div>{changeLanguage('Translate me', locale, setLocale)}</div>
</div>
);
};
and
const changeLanguage = (lang, state, setState) => {
fetch(localizationEndpointForThatLanguage)
.then(response => response.json())
.then(data => {
setState({ ...state, phrases: data });
});
};
I am fine with having to handoff context values throughout the app to make things work. I'd like help understanding why my translateString function does not require me to pass context values to it.

Resources