GithubHelp home page GithubHelp logo

royderks / react-context-hooks-workshop Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 6.0 625 KB

Workshop about State-management in React with Context and Hooks

Home Page: https://www.slideshare.net/RoyDerks1/workshop-statemanagement-in-react-with-context-and-hooks

License: MIT License

HTML 14.44% JavaScript 85.56%
react state-management hooks

react-context-hooks-workshop's Introduction

State-management in React with Context and Hooks

Get started

yarn && yarn start

This project uses a JSON-based API, using My JSON Server, which uses the file db.json to create a REST API. You can use GET, POST, PUT, PATCH, and DELETE. But keep in mind that changes aren't persisted between calls.

Libraries used

Exercises

  1. Try running this application, what do you see? To display data on this page (/), you need to request the data from the API and put it in a local state object e.g. with useState.

Hint: the hotel information is available on https://my-json-server.typicode.com/royderks/react-context-hooks-workshop/hotels.

  1. Now the application is displaying a list of hotels, when you click on one of the hotels you'll navigate to a detail page. You need to fetch the data from an individual hotel on this page by adding one or multiple useState Hooks to Detail.js.

Hint: Use the match object from React Router to get the hotel id from the URL.

  1. Also, get the reviews for this hotel from the REST API. You can render these inside the ReviewWrapper component, and use the already existingReviewItem` component to display them.

Hint: Have a look at the db.json file to find all endpoints for the REST API.

  1. Create a new file called HotelsContext.js and instead of adding the data for a hotel to a local state object in Hotels.js, put it in a Context object. Consume the Context for the hotels in Hotels.js. You can consume this same Context in Detail.js so that you only need to request to get the hotel data once when navigating between different pages.

Hint: Where should you import this file to be able to access it from both the Hotels.js and Detail.js files?

  1. The logic for the reviews can also be added to the Context for the hotel data, make the changes to also consume this information from Detail.js. Keep in mind that you only want to request the reviews for the hotel detail page that you currently have open.

  2. Everything we did so far is based on Hooks, so let's create a custom Hook. You can call this Hook useHotelsContext and use it instead of the built-in useContext Hook from React.

  3. Split the data about the hotel reviews into a different Context object, in a new file called ReviewsContext.js. The data about the reviews can be removed from the HotelsContextProvider afterward.

  4. As your application grows in size, it's more efficient to use the useReducer Hook to handle state updates than the useState Hook. Refactor both the Context for hotels and reviews to use useReducer instead. If you're familiar with Redux you can use a comparable pattern to handle changes in your state.

Hint: Wrap the logic to do data fetching into separate functions in the Context files, and move all useEffect Hooks to the components that request the data.

  1. When clicking on a hotel, you might have seen the "Add review" button. Submitting a review on this page sends a POST request to the mock REST API. No actual data is saved as it's a mock REST API, but a success message is returned. Can you still somehow "save" the request in the Context for reviews?

BONUS: Show a success message when a new review has been added.

  1. Refactor the logic to retrieve the hotel for the detail page that you currently have open, so that not all hotels are being requested from the API but just the opened hotel. This is particularly useful if the user visits the detail page directly, and doesn't navigate there from the main page.

  2. To give the idea of a "global Context" you can compose the HotelsContextProvider and the ReviewsContextProvider together. You can do this in the file GlobalContext.js.

Hint: Use the children object to make it possible to wrap a component.

react-context-hooks-workshop's People

Contributors

royderks avatar

Stargazers

 avatar

Watchers

 avatar

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.