GithubHelp home page GithubHelp logo

arh-03's Introduction

arh-03

useContext hook

The useContext hook solves for sharing state between components without prop drilling.

Context also has the unique ability to be scoped to a specific section of the React component tree. A common mistake of context (and generally any "application" state) is to make it globally available anywhere in your application when it's actually only needed to be available in a part of the app (like a single page). Keeping a context value scoped to the area that needs it most has improved performance and maintainability characteristics. ~Kent C Dodds

import * as React from 'react'

const FooContext = React.createContext()

function FooDisplay() {
  const foo = useFoo() //Return value is derived from the value prop passed into FooContext.Provider
  return <div>Foo is: {foo}</div>
}

//Custom useContext hook
function useFoo() {
  const context = React.useContext(FooContext)
  //Error handling incase the custom hook is used without the context provider
  try {
    if(!context) throw new Error("useFoo must be used within FooContextProvider")
    return context
  } catch(err) {
    console.error(err)
    return null
  }
}

function FooContextProvider(props) {
  const displayText = "I am foo"
  return <FooContext.Provider value={displayText} props={...props}/> //The value prop contains the state accessible by the child components
}

ReactDOM.render(
  <FooContextProvider>
    <FooDisplay /> //This component has access to the state held in FooContextProvider without having to explicitly pass state in through props
  </FooContextProvider>,
  document.getElementById('root'),
)

// renders <div>Foo is: I am foo</div>

arh-03's People

Contributors

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