window.scrollTo not working inside useEffect – reactJS

I am trying to execute window.scrollTo on the first render using useEffect function. I am storing the position inside localStorage, but it is not working. Does not work on first page render: useEffect(() => { window.scrollTo(0, localStorage.getItem(‘position’)); }, []); It is worth noting that if I put the scroll function inside a function and call… Read More window.scrollTo not working inside useEffect – reactJS

Uncaught TypeError: Cannot read properties of undefined (reading 'id')

I get undefined because item from cartitems is not undefined, how can I fix it? 1. import React,{useState} from ‘react’ import {products} from ‘./data’ function app() { const [cartitems, setCartitems] = useState([]) const onAddToCart = (product)=>{ const exist = cartitems.find((item)=> { return product.id == item.id }) if(exist){ setCartitems(cartitems.map((item)=>{ item.id == product.id ? {…exist, qnty: exist.qnty… Read More Uncaught TypeError: Cannot read properties of undefined (reading 'id')

UseEffect Unexpected Reserved Word 'await'

I’m trying to convert my class component to a function component. I believe instead of ComponentDidMount we need to use useEffect: useEffect(() => { const { data: ServerResponse } = await axios.get(‘http://127.0.0.1:8000/api/tweets/’) console.log(ServerResponse) setPosts(ServerResponse) }, []) But I get the following error: Unexpected reserved word ‘await’ >Solution : If your want to use "await" keyword… Read More UseEffect Unexpected Reserved Word 'await'

Why is it not filling the useState whin i get the data?

I folowed tthis page: https://blog.logrocket.com/using-chart-js-react/ The data is loaded into the data and I see in console but the data is not loaded into the useState import React, { useEffect, useState } from "react"; import budgetApi from "../api/budgetApi"; import { Bar } from "react-chartjs-2"; const BarChart = () => { const [chartData, setChartData] = useState({});… Read More Why is it not filling the useState whin i get the data?

How can I request the value I get from the user to the backend api using react?

I wrote a simple api using spring boot. For example, when I send http://localhost:8080/rps?choise=rock request, the response is "You choose Rock and CPU choose Paper ,You Lose!" it turns. I created a simple interface using ReactJs, there are 3 buttons on the screen, rock, paper and scissors. When the user clicks on one of them,… Read More How can I request the value I get from the user to the backend api using react?