Skip to Content
Modern Front-End Development for Rails
book

Modern Front-End Development for Rails

by Noel Rappin
June 2021
Intermediate to advanced
398 pages
9h 35m
English
Pragmatic Bookshelf
Content preview from Modern Front-End Development for Rails

Using Context to Share State

For our app, we’re going to use two React hooks to help us share state. The first hook we’re going to use is useContext, which lets us use a feature of React called a context. In React, contexts allow us to share global data among components without passing that data via props.

To use a context, we surround our code with a special JSX component called a context provider. The context provider is initialized with a value, and then any component inside that provider, no matter how many levels down, can use the useContext hook to give that component inside access to the data in the context.

The specific data we want to share in our context is a reducer function that will provide our common state and a dispatch function ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern Front-End Development for Rails, 2nd Edition

Modern Front-End Development for Rails, 2nd Edition

Noel Rappin
Agile Web Development with Rails 6

Agile Web Development with Rails 6

Sam Ruby, David B. Copeland, Dave Thomas

Publisher Resources

ISBN: 9781680508758Errata Page