GithubHelp home page GithubHelp logo

reactcontextexample's Introduction

React Context Example

This small project should explain how React Context works.

Technologies

  • NextJS
  • Material-UI
  • React Context

Functionality

  • Standard use of Context
  • Using Context in the Consumer with the Hook
  • Setting Context values from a Consumer (function in ReactContext)

Running the project

Standard NextJS strartup: Go in the folder and type

npm run dev

How React Context works

Sources

This StackOverflow article

userContext

Check out /lib/userContext.js

This is the file setting up the Context.

Nothing fancy, just a value. The second param (the function) is not given here. No problem (but Typescript will loose its marbles on this probably)

SetupUI

Check out ui/setup/setupui.js

There is the Context.Provider.

You must also put the useState there holding the variable of the Context.

The second param on the Context is a function which is able to set a new value. In this case simply the second param of the useState. (Again, not set up in userContext.js, but you can only do that in JS, not TS)

Initial State use useState is passed on the Context

Header

Check out ui/header/header.js

There we make use of the useContext hook to get the complete userContext.

We only destructure the value ('user') because we don't need the second param here since we're not setting the user in the header.js.

Just put the user on the screen (with a fancy ShowName here)

Settings

This is a screen where we want to manipulate the value of the context.

Therefore we desctructure the 'setUser' function.

Since this is a setter from useState, we're using it as such.

So, button click sets just another hardcoded name.

Since it is state, rerender is done on the screen.

reactcontextexample's People

Contributors

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