Code Monkey home page Code Monkey logo

react-hooks-state-and-events-lab's Introduction

React State Lab

Learning Goals

  • Update state based on events
  • Work with multiple state variables together

Introduction

In the labs for this section, we'll be working on a grocery list app. Some of this app is already built out using static versions of the components. We'll be using state and events to make our app dynamic.

The components you have to work with are in the src/components folder. Start by examining these components and draw out your component hierarchy to see how data can be passed from one component to another.

In this lab, you'll update state and get more practice with the useState hook.

It's recommended that you run npm start and work on this in the browser before running tests. Try to get your app to match the demo! Once you've built out the components below, run npm test to see if your code passes the tests.

demo

Deliverables

Dark Mode Toggle

In the App component, there is a button for toggling between dark mode and light mode. Using the useState hook, create a state variable in the App component. Then, use that variable to determine if our app should be in dark mode or light mode.

You will also need to add an event handler to the dark mode button, and update state when the button is clicked.

The actual functionality of changing our app's theme is handled in CSS, so all you have to do is set up the code to update the className of the div based on your state variable:

<div className="App dark">
{/* for dark mode */}
</div>

<div className="App light">
{/* for light mode */}
</div>

Add to Cart

In the Item component, when the user clicks the <button> element, the item should be added to their virtual cart. The way we'll show the user that the item is in the cart is by changing the className on the <li> element:

<li className="in-cart">
{/* the item is in the cart */}
</li>

<li className="">
{/* the item is NOT in the cart */}
</li>

If the item is not in the cart, the <button> element's text should read "Add to Cart", and if the item is in the cart, the <button> element's text should read "Remove From Cart". Naturally, you'll also need to add state to the Item component to solve this deliverable!

Filter

In the ShoppingList component, there is a <select> element that will allow us to filter the list of items by category.

Use the useState hook to create a state variable called selectedCategory for keeping track of the selected value from this <select> element. When the value of the <select> element is changed, update state.

You should also use the selectedCategory variable to determine which items to display in the shopping list. You'll need some way of filtering the array of items based on the selectedCategory.

Resources

react-hooks-state-and-events-lab's People

Contributors

annjohn avatar gehrig-barnes avatar gj avatar graciemcguire avatar ihollander avatar jlboba avatar kjleitz avatar lizbur10 avatar lukeghenco avatar maxwellbenton avatar pletcher avatar sbalayan1 avatar sylwiavargas avatar thomastuts avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-hooks-state-and-events-lab's Issues

After Filtering Out Everything But Dairy The Produce Are Removed From The Cart When User Did Not Remove It

Canvas Link

https://learning.flatironschool.com/courses/6558/assignments/242873?module_item_id=571386

Concern

Although my program so far matches the DEMO, the DEMO has a bug. Which means my code has a bug right now. Pay Close attention to Pomegranate and Lettuce.

*Yogurt
*Pomegranate
*Lettuce
String Cheese
Cookies

*in cart initially

NOW filter out all but dairy:

*Yogurt
String Cheese

NOW show all:

*Yogurt
Pomegranate
Lettuce
String Cheese
Cookies

ALTERNATIVELY:

*Yogurt
*Pomegranate
*Lettuce
String Cheese
Cookies

*in cart initially

NOW filter out all but dessert:

Cookies

NOW show all:

Yogurt
Pomegranate
Lettuce
String Cheese
Cookies

AS THE USER, I DID NOT REMOVE THE ITEMS FROM THE CART.

Additional Context

GOAL IS TO MATCH DEMO. DEMO HAS A BUG WHERE WHEN YOU SHOW ONLY ITEMS IN A CATEGORY THAT DOES NOT HAVE THE ITEMS IN THE CART, THE ITEMS IN THE CART ARE REMOVED FROM THE CART WHEN THE FILTER REGENERATES THE ITEMS.

Suggested Changes

Instead of filtering out the items, we could say style.display="none" for the items to be excluded; and of course style.display = "block", but in React it would be different because you must change the display state to get the item to regenerate...

"Add to Cart" Bad Instructions

Canvas Link

https://learning.flatironschool.com/courses/4609/assignments/150073?module_item_id=318041

Concern

For the "Add to Cart" section, it instructs that the <li> className switch between "in-cart" and "". However, in "./src/data/index.css" I found that this wasn't correct, as it showed:
`.Items li.in-cart {
background: var(--background--light);
}

.Items button.add {
background-color: var(--yellow);
}`

Additional Context

React State Proof A
React State Proof B

Suggested Changes

All that is needed to fix this issue would be to edit the content of the code snippet.

Some of the instructions aren't included

Thanks for raising this issue! Future learners thank you for your diligence. In
order to help the curriculum team address the problem, please use this template
to submit your feedback. We'll work on addressing the issue as soon as we can.

Please fill out as much of the information below as you can (it's ok if you
don't fill out every section). The more context we have, the easier it will be
to fix your issue!

Note: you should only raise issues related to the contents of this lesson.
If you have questions about your code or need help troubleshooting, reach out to
an instructor/your peers.


Link to Canvas

Add a link to the assignment in Canvas here.

What should be changed?

Nowhere in the instructions does it mention that you also need to update the class on the button in the Item.js file, so that you can have the button go between "Add to Cart" and "Remove from Cart" in order to make the project look like the GIF included on Canvas.

Suggest your change here. Let us know what section or line of the Readme needs
to be updated, and any proposed corrections. Include the line number(s) if
possible.

Additional context

Add any other context about the problem here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.