Skip to content

Commit

Permalink
Use useContext for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVanDerSchans committed Apr 7, 2024
1 parent e173f95 commit 623bd9d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/components/app/App.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { useState } from 'react'
import { useContext, useState } from 'react'
import { Header } from '../Header/Header'
import { products as initialProducts } from '../../mocks/products.json'
import { Products } from '../Products/Products'
import { Footer } from '../Footer/Footer'
import { IS_DEVELOPMENT } from '../../config'
import './App.css'
import { FiltersContext } from '../../context/filters'

function useFilters() {
const [filters, setFilters] = useState({
category: 'all',
minPrice: 0,
})
// const [filters, setFilters] = useState({
// category: 'all',
// minPrice: 0,
// })

const { filters, setFilters } = useContext(FiltersContext)

const filterProducts = (products) => {
return products.filter((product) => {
Expand Down
11 changes: 8 additions & 3 deletions src/context/filters.js → src/context/filters.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { createContext } from 'react'
import { createContext, useState } from 'react'

export const FiltersContext = createContext()

export function FiltersProvider({ children }) {
const [filters, setFilters] = useState({
category: 'all',
minPrice: 0,
})

return (
<FiltersContext.Provider
value={{
category: 'all',
minPrice: 0,
filters,
setFilters,
}}
>
{children}
Expand Down
6 changes: 3 additions & 3 deletions src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import { FiltersProvider } from './context/filters'
import ReactDOM from 'react-dom/client'
import App from './components/app/App'
import './main.css'

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<FiltersProvider>
<App />
</React.StrictMode>
</FiltersProvider>
)

0 comments on commit 623bd9d

Please sign in to comment.