Giter Site home page Giter Site logo

jcjmcclean / react-simple-hooks-example Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 8 KB

Example of using the state hook with react

Home Page: https://codesandbox.io/s/github/jcjmcclean/react-local-storage-example

HTML 40.75% TypeScript 58.30% CSS 0.95%

react-simple-hooks-example's Introduction

Simple hooks example

Overview

Following this example, you can see how to handle API requests for data which is used in a single place. We cover sharing data from parent (ColoursPage) to child (ColoursListComponent) component through input props and requesting data from an external function.

Sample code

Code for this example can be found here:

Files

This example contains a page component (which is included in the main app component), a context and a list component.

colours.context.ts ColoursContext

This provides context (data/state/functions) to components (or pages) within the colours module.

Here is a quick breakdown of this file:

Creating the ColoursContext function and returning an object.

function ColoursContext() {
  return {
    ...
  };
}

Creating the getColours method, which calls an API endpoint and returns a parsed response.

getColours: () => {
  return axios.get("https://reqres.in/api/colours").then(res => {
    return res.data.data;
  });
}

colours.page.tsx ColoursPage

This is the outer template, which is included in the main app component.

Here is a quick breakdown of this file:

Creating the ColoursPage function component.

function ColoursPage() {
  ...
}

Calling ColoursContext, so we can call it's inner functions later.

 const coloursContext = ColoursContext();

Using React hooks to create a colours state on the page.

 const [colours, setColours] = React.useState([]);

Code inside React.useEffect runs first when the component has mounted. The empty array after the function closes tells React to only run this once. You can use this array to specify values for React to watch, then when these values change, it will re-run the code in useEffect. See the React docs for more.

 React.useEffect(() => {
 }, []);

We call getColours from our instance of ColoursContext and wait for the response. We then update our colours hook, using setColours to the response value.

 coloursContext.getColours().then(response => {
   setColours(response);
 });

Finally we render the component. We set a colours prop value on ColoursListComponent to the value of colours in our ColoursPage state.

 // Render coloursPage component
 return (
   <div>
     <h1>Colours page</h1>
     <ColoursListComponent colours={colours} />
   </div>
 );

colours.list.component.tsx ColoursListComponent

Creating the ColoursListComponent function component, defining that it accepts input props and returning JSX to be rendered.

function ColoursListComponent(props: any) {
  return (
    <ul>
      ...
    </ul>
  );
}

Looping through the colours array and output each colour as a list item.

{props.colours.map(function(colour, index) {
  return (
    <li key={index}>
      {colour.name} {colour.color}
    </li>
  );
})}

react-simple-hooks-example's People

Contributors

jcjmcclean avatar

Watchers

 avatar  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.