Skip to content

Used State to Make Page Dynamic #22

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

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
13 changes: 7 additions & 6 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React from "react";
import React, { useState } from "react";
import ShoppingList from "./ShoppingList";
import itemData from "../data/items";

function App() {

// replace 'false' with a state variable that can be toggled between true and false
// this will be used for the Dark Mode Toggle feature
const appClass = false ? "App dark" : "App light"
const [count, setCount] = useState(false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of a better name than count? What is really being stored in that variable?

const appClass = count ? "App dark" : "App light"
const buttonText = count ? 'Dark Mode' : 'Light Mode'

return (
<div className={appClass}>
<header>
<h2>Shopster</h2>
<button>Dark Mode</button>
<button onClick={ () => setCount(count => !count) }>
{ buttonText }
</button>
</header>
<ShoppingList items={itemData} />
</div>
Expand Down
11 changes: 8 additions & 3 deletions src/components/Item.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from "react";
import React, { useState } from "react";

function Item({ name, category }) {
const [count, setCount] = useState(false)
const updateClass = count ? 'in-cart' : ''
const updateButtonText = count ? 'Remove From Cart' : 'Add to Cart'
Comment on lines +5 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you prefix with update, that implies that something is updated. While that's kind of true in this case, it's not entirely true and there is a better name. Something like itemClass implies that it will be returning a a class name for the item, and buttonClass is less redundant. It's subjective, but what do you think?

return (
<li className="">
<li className={ updateClass }>
<span>{name}</span>
<span className="category">{category}</span>
<button className="add">Add to Cart</button>
<button className="add" onClick={() => setCount(count => !count)}>
{ updateButtonText }
</button>
</li>
);
}
Expand Down
14 changes: 9 additions & 5 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import React from "react";
import React, { useState } from "react";
import Item from "./Item";

function ShoppingList({ items }) {

const [count, setCount] = useState('All')
const filterByCategory = count === 'All' ? items : items.filter(item => item.category === count)

return (
<div className="ShoppingList">
<div className="Filter">
<select name="filter">
<select onChange={(event) => setCount(event.target.value)} name="filter">
<option value="All">Filter by category</option>
<option value="Produce">Produce</option>
<option value="Dairy">Dairy</option>
<option value="Dessert">Dessert</option>
</select>
</div>
<ul className="Items">
{items.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
))}
{ filterByCategory.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
)) }
</ul>
</div>
);
Expand Down