Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Header = ({ location }) => {
const [state, dispatch] = useContext(CartContext)
const [miscState, miscDispatch] = useContext(MiscContext)

console.log(location)
// console.log(location)

const getActiveSidebar = () => location.state?.activeSidebar

Expand Down
7 changes: 7 additions & 0 deletions src/components/product/productBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react"

import Button from "../button"
import AsInputNumber from "../inputNumber"
import useHasMountedAndHasValue from "../useHasMountedAndHasValue"
import { round } from "../../utils"

const AddButton = (props) => {
Expand Down Expand Up @@ -87,6 +88,11 @@ const ProductBase = (props) => {
data.price = round(data.price)
const [count, setCount] = useState(countInCart || 1)

// Two-pass rendering technique, see:
// https://github.com/gatsbyjs/gatsby/issues/17914
// https://joshwcomeau.com/react/the-perils-of-rehydration/
const hasMountedAndHasValue = useHasMountedAndHasValue(countInCart)

useEffect(() => {
if (countInCart) setCount(countInCart)
}, [countInCart])
Expand All @@ -99,6 +105,7 @@ const ProductBase = (props) => {
countInCart,
ToggleButton,
InputNumber,
hasMountedAndHasValue,
}

return <View {...viewProps} />
Expand Down
11 changes: 2 additions & 9 deletions src/components/product/productCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Link } from "gatsby"
import Img from "gatsby-image"

import Image from "../image"
import useHasMounted from "../useHasMounted"

const ProductCard = (props) => {
const {
Expand All @@ -14,6 +13,7 @@ const ProductCard = (props) => {
countInCart,
InputNumber,
ToggleButton,
hasMountedAndHasValue
} = props

const image = data.images ? (
Expand All @@ -22,15 +22,8 @@ const ProductCard = (props) => {
<Image />
)

//see: https://github.com/gatsbyjs/gatsby/issues/17914
// https://joshwcomeau.com/react/the-perils-of-rehydration/
const hasMounted = useHasMounted()

return (
<div
key={hasMounted}
className={`productCard ${countInCart ? "active" : ""}`}
>
<div key={hasMountedAndHasValue} className={`productCard ${countInCart ? "active" : ""}`}>
<Link to={`/${data.slug}/`}>{image}</Link>
<div className="subtitle row">
<span className="price">
Expand Down
13 changes: 13 additions & 0 deletions src/components/useHasMountedAndHasValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react"

const useHasMountedAndHasValue = (value) => {
const [hasMountedAndHasValue, setHasMountedAndHasValue] = React.useState(
false
)
React.useEffect(() => {
if (value) setHasMountedAndHasValue(true)
}, [])
return hasMountedAndHasValue
}

export default useHasMountedAndHasValue