Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose 'audience' to entire app using React Context #62

Open
DomVinyard opened this issue Mar 3, 2023 · 1 comment
Open

Expose 'audience' to entire app using React Context #62

DomVinyard opened this issue Mar 3, 2023 · 1 comment
Assignees
Labels
all must approve Approval required from entire team to merge

Comments

@DomVinyard
Copy link
Collaborator

DomVinyard commented Mar 3, 2023

Multiple parts of our application needs to know when the audience level (5/10/Adult) changes. The TopicCard and RelationCards need to know which content to render, the header needs to know which level to display. It's likely in the future other parts of the application will need to know this too.

One option would be to move the logic into App.js and then pass it down as props to every component, but instead we are going to set the audience level using the React Context API so it is available throughout the application.

This is a good intro to the React Context API: https://www.youtube.com/watch?v=t9WmZFnE6Hg

  • Note to assignees, please tag everybody to review this PR and ask all reviewers to watch the video above (or some other material) to understand this PR before approving

Acceptance Criteria

  • Create a new context called AudienceContext
  • AudienceContext should have one piece of state called [audience, setAudience]
  • AudienceContext should expose both the setter (setAudience) and the getter (audience)
  • Header should be updated to read and write its values to AudienceContext
  • TopicPage should be updated to read its value from AudienceContext
  • When Audience changes, localstorage should be updated from inside AudienceContext

Example

We should be able to access the data from any part of the application:

import React, { useContext } from 'react';
import AudienceContext from './AudienceContext';

function MyComponent() {
  const { audience } = useContext(AudienceContext);

  return (
    <div>
      <p>The audience is currently set to {audience}</p>
    </div>
  );
}
@DomVinyard DomVinyard changed the title Expose audience level (age) to entire application Expose audience level (age) to entire app using React Context Mar 3, 2023
@DomVinyard DomVinyard changed the title Expose audience level (age) to entire app using React Context Expose 'audience' to entire app using React Context Mar 3, 2023
@DomVinyard DomVinyard added the all must approve Approval required from entire team to merge label Mar 6, 2023
@MariamM110
Copy link
Collaborator

A more updated and useful article to read
https://beta.reactjs.org/reference/react/useContext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
all must approve Approval required from entire team to merge
Projects
None yet
Development

No branches or pull requests

3 participants