Solid context doesnt work

Learning solid using docs and i use the docs example to create a context but for some reason this error appears, can someone help or give a sugestionon how i can fix? mycode:

import { createContext, useContext } from "solid-js"
import { createStore } from "solid-js/store"

const CartContext = createContext({})

type CartItem = {
item: string,
amount: number
}

type CartProviderProps = {
children: any // dunno how to type children ;-; docs doest help
}


export function CartProvider({
children
}: CartProviderProps) {

const [cartData, setCartData] = createStore<CartItem[]>([])

// Actions
function addToCart(item: string, amount: number) {

setCartData([...cartData, {
item, amount
}])
}
function removeFromCart(itemId: string) {

setCartData(cartData.filter(item => item.item !== itemId))
}
function changeItemAmount(itemId: string, amount: number) {

setCartData(cartData.map(item => {
if (item.item === itemId) {
return {
...item,
amount
}
}

return item
}))
}

return (
<CartContext.Provider value={{
cartData,
addToCart,
removeFromCart,
changeItemAmount,
}}>
{children}
</CartContext.Provider>
)
}

export function useCart() { return useContext(CartContext) }

import { createContext, useContext } from "solid-js"
import { createStore } from "solid-js/store"

const CartContext = createContext({})

type CartItem = {
item: string,
amount: number
}

type CartProviderProps = {
children: any // dunno how to type children ;-; docs doest help
}


export function CartProvider({
children
}: CartProviderProps) {

const [cartData, setCartData] = createStore<CartItem[]>([])

// Actions
function addToCart(item: string, amount: number) {

setCartData([...cartData, {
item, amount
}])
}
function removeFromCart(itemId: string) {

setCartData(cartData.filter(item => item.item !== itemId))
}
function changeItemAmount(itemId: string, amount: number) {

setCartData(cartData.map(item => {
if (item.item === itemId) {
return {
...item,
amount
}
}

return item
}))
}

return (
<CartContext.Provider value={{
cartData,
addToCart,
removeFromCart,
changeItemAmount,
}}>
{children}
</CartContext.Provider>
)
}

export function useCart() { return useContext(CartContext) }
4 Replies
Alex Lohr
Alex Lohr11mo ago
The variables for contexts are supposed to be lower case. Otherwise they are wrapped like normal components.
Carere
Carere11mo ago
Should the extension be tsx ?
o gabriel
o gabriel11mo ago
thank you very much for the help, everyone in the company is laughing a lot here for my mistake A common mistake by me ;-;
Carere
Carere11mo ago
Haha no PB 😅, you're welcome 😁
Want results from more Discord servers?
Add your server
More Posts
Reactivity inside closure in return valueI have a function with a store, singals etc. This function returns an object, that object contains aError [RollupError]: "assign" is not exported by "node_modules/solid-js/web/dist/server.js"When trying to build my solid-start app using `solid-start build` I get the following error: ``` fisolid-router not updating when going the same page but with different parameterso/ so i have this in `/routines/[id].tsx`: ```tsx export default function EditRoutine() { const updating a member inside another member inside an array inside a store using an indexo/ so i have data structured as follows, which is inside a store: ```js { actions: [ { FileRoutes duplicate hydration and not displaying dynamic routeif i used fileroutes the nested route is not working well, but if i used not fileroutes it behave How to use SolidStart on existing SolidJs project in order to benefit of File Based Routing ?PS: I use SolidJs only for SPA, so only client-side ? Is it possible to use SolidStart only for clieHow do I modify a member of an object in an array in a SolidJS Store using the SetStoreFunction?(Re-post from https://stackoverflow.com/questions/76843586/how-do-i-modify-a-member-of-an-object-in-useParams() not changing when switching pages?i'm getting really odd behavior ```tsx export default function EditRoutine() { const params = usComponent doesn't rerender when prop changesI have the component below: ```ts export default function FolderContents(props: { path: string }) { make object no longer reactiveo/ so i have this piece of code ```ts // routines: [] setRoutines(v => [...v, routine]) // routines: