currently I am working on a project and find myself in a bothersome situation. Is it normal for the components to load before useEffect?
On my page I want to implement pagination on sidebar. I have state which will determine current page and that state which is number will be an index which will take nested array and show the content. However data is an array without nested arrays and firstly I should convert that array into array with nested ones. Because that, I want to run that inside side effect because it should only be done at initial loading. Now I am trying with hardcoded values and later will set dynamic ones.
The problem now is that useEffect doesn't run first and the rest of code actually executes itself before useEffect and I got errors like "MenuList.js:173 Uncaught TypeError: DUMMY_FOOD[0].map is not a function" and ect. I know that my array is not in right format hence if I log without this [0] it works.
What is problem?
Code:
const MenuList = () => { const navigate = useNavigate(); const location = useLocation(); const params = useParams(); const [page, setPate] = useState(0); useEffect(() => { const pages = Math.ceil(DUMMY_FOOD.length / 5); const arr = []; let helpArr = []; let c = 0; for (let i = 0; i < pages; i++) { for (let j = c; j < c + 5; j++) { console.log("picapicapiac"); helpArr.push(DUMMY_FOOD[j]); } c += 5; arr.push(helpArr); helpArr = []; } console.log(arr); DUMMY_FOOD = arr; }, []); console.log(DUMMY_FOOD); const queryPrams = new URLSearchParams(location.search); const sort = queryPrams.get("sort"); const onNextPageHandler = () => {}; const onPreviousPageHandler = () => {}; const onSortPageHandler = () => { navigate(`/menu/${params.foodId}/?sort=${sort === "asc" ? "desc" : "asc"}`); sort === "asc" ? (DUMMY_FOOD = DUMMY_FOOD.sort((a, b) => a.foodPrice - b.foodPrice)) : (DUMMY_FOOD = DUMMY_FOOD.sort((a, b) => b.foodPrice - a.foodPrice)); }; return ( <Fragment> <div className={classes["menu-list"]}> {DUMMY_FOOD.map((foodObj) => ( <MenuItem key={foodObj.id} foodObj={foodObj} /> ))} </div> <div className={classes["menu-list__buttons"]}> <Button type="button" onClick={onPreviousPageHandler}> Page 2 </Button> <Button type="button" onClick={onSortPageHandler}> {sort === "asc" ? `Descending ↑` : `Ascending ↓`} </Button> <Button type="button" onClick={onNextPageHandler}> Page 3 </Button> </div> </Fragment> ); }; export default MenuList;