Untitled conversation
- User
[{ "resource": "/c:/Users/alexa/Desktop/sae-fest-iuto-front/src/App.tsx", "owner": "typescript", "code": "2451", "severity": 8, "message": "Cannot redeclare block-scoped variable 'cart'.", "source": "ts", "startLineNumber": 26, "startColumn": 10, "endLineNumber": 26, "endColumn": 14 }]import './index.css' import './components/nav/Navbar' import Navbar from './components/nav/Navbar' import { AnimatePresence } from 'framer-motion' import { Route, Routes, useLocation } from 'react-router-dom' import Accueil from './pages/Accueil/Accueil' import { createContext, useEffect, useState } from 'react' import Programmation from './pages/Programmation/Programmation' import PageArtiste from './pages/Artiste/PageArtiste' import Billeterie from './pages/Billeterie/Billeterie' import Faq from './pages/faq/Faq' import { getCookie } from './cookies/CookiesLib'
interface cartContext{ cart: any[], setCart: (cart: any[]) => void, }
export const CartContext = createContext({ cart: [], setCart: () => {}, })
function App() { const [cart, setCart] = useState<any[]>(() => getCookie('cart') || []); const [cart, setCart] = useState<any[]>([]); const [isNavInFocus, setIsNavInFocus] = useState(false) const[isNavTransparent, setIsNavTransparent] = useState(true); const location = useLocation();
useEffect(() => {
}, []);
return ( <CartContext.Provider value = {{cart, setCart}}>
<Routes location={location} key={location.key}> <Route path="/" element={<Accueil isNavInFocus={isNavInFocus} setIsNavTransparent={setIsNavTransparent} />} /> <Route path="/programmation" element={<Programmation isNavInFocus={isNavInFocus} setIsNavTransparent={setIsNavTransparent} />} /> <Route path="/billeterie" element={<Billeterie isNavInFocus={isNavInFocus} setIsNavTransparent={setIsNavTransparent} />} /> <Route path="/faq" element={<Faq isNavInFocus={isNavInFocus} setIsNavTransparent={setIsNavTransparent} />}/> <Route path="/artiste" element={<PageArtiste />}/> </Routes> </AnimatePresence> </CartContext.Provider>) }
export default App import { motion } from 'framer-motion'; import { useContext, useState } from 'react'; import Button from '../components/form/Button'; import { getCookie, setCookie } from '../cookies/CookiesLib.tsx'; import { CartContext } from '../App';
const initialDays = { '20Juillet': false, '21Juillet': false, '22Juillet': false };
type Props = { id: number; title: string; price: number|string; nbTicket: number; isForfait?: boolean; };
export default function TicketCard({ id, title, price, nbTicket, isForfait }: Props) { const [isOpen, setIsOpen] = useState(false); const [tickets, setTickets] = useState(nbTicket); const [rotation, setRotation] = useState(0); const [days, setDays] = useState(initialDays); const { cart, setCart } = useContext(CartContext);
const handleTicketChange = (newTickets: number, event: React.MouseEvent) => { event.stopPropagation(); setTickets(newTickets); }; const addToCartHandler = () => { const selectedDays = isForfait ? Object.entries(days).filter(([_, isChosen]) => isChosen).map(([day, _]) => day) : [];
- Assistant
It looks like you've pasted an error report for a TypeScript issue along with a snippet of a React component written in TypeScript. The error is as follows:
"Cannot redeclare block-scoped variable 'cart'."This error is occurring because you're trying to declare a variable with the same name "cart" more than once within the same block scope. Let's fix it by removing the duplicate declaration:
function App() { const [cart, setCart] = useState<any[]>(() => getCookie('cart') || []); // The following line should be removed because it re-declares 'cart' and 'setCart' // which were already declared in the previous line // const [cart, setCart] = useState<any[]>([]); const [isNavInFocus, setIsNavInFocus] = useState(false) const [isNavTransparent, setIsNavTransparent] = useState(true); const location = useLocation(); // Rest of your App component... } - User
- Assistant
The reason why the cart notification count is not updating without a refresh might be because the state is only being set once when the component mounts. In React, using
useEffectwith an empty dependency array[]means that it will run only once after the initial render. - User
- Assistant