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

react2-week1/class-exercises #315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BJORN-VINTHER
Copy link
Collaborator

@BJORN-VINTHER BJORN-VINTHER commented Jan 30, 2024

❗ This PR is not required to be reviewed. ❗

This PR contains the code from the React 2 lesson 1:

  • The theme project where we learned how to use contexts and insert icons
  • The hyf project where we used props.children to recreate the HackYourFuture website

@github-actions github-actions bot changed the title Added class exercises react2-week1/class-exercises Jan 30, 2024
Comment on lines +3 to +14
export default function InfoPanel(props) {
return (
<>
<div className="container">
<img className="my-img" src={props.imageUrl}></img>
<div className="content">
<h2 className="my-title">{props.title}</h2>
{props.children}
</div>
</div>
</>
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we used the props.children to tell React where we want the custom HTML to appear. In our case we placed it underneath out heading.

Comment on lines +18 to +27
<InfoPanel
imageUrl={
"https://images.ctfassets.net/ljpkryr3szrz/3lNk9flnJXV9VBtCxTeB5c/7ffee160ffe05d43bff4cff636cccd2d/IMG_7644.jpg"
}
title="The program"
>
We believe in a world in which education and quality jobs are
accessible for all. This is why we have designed a free 34 week
program for newcomers in tech.
</InfoPanel>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we insert our component InfoPanel in our page. Notice how we add the text between the start- and endtag. This will become the props.children inside the component.

<InfoPanel> Any custom JSX can go in here </InfoPanel>

Copy link
Collaborator Author

@BJORN-VINTHER BJORN-VINTHER Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember that React will only display images and icons that are placed inside the /public folder.

Comment on lines +12 to +16
const [theme, setTheme] = useState("dark");

return (
<>
<ThemeContext.Provider value={theme}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are providing the value for our global theme for the ThemeContext.

<ThemeContext.Provider value={theme}>
Any components inside here can access the value of ThemeContext
</ThemeContext.Provider>

Comment on lines +1 to +3
import { createContext } from "react";

export const ThemeContext = createContext("light");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are creating a ThemeContext with the initial value "light"

Comment on lines +4 to +8
export default function ThemedHeader({ title }) {
const theme = useContext(ThemeContext);
const classes = `themed-header ` + theme;
return <h2 className={classes}>{title}</h2>;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we read out the value of the ThemeContext in one of our components. Notice how we don't have to pass it via props.

Comment on lines +32 to +35
<img
src={process.env.PUBLIC_URL + "sun.svg"}
alt="light theme"
/>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you want to include an image or icon from the public folder you have to use the url:

process.env.PUBLIC_URL + {image path inside public folder}

Comment on lines +45 to +75
<ThemeContext.Provider value={"dark"}>
<ThemeContainer
title="Dark theme"
buttonText="Use Dark theme"
onButtonClick={(x) => setTheme("dark")}
>
<img
width={50}
height={50}
src={process.env.PUBLIC_URL + "moon.png"}
alt="random"
/>
<ThemedText text="Dark themes are great for low-dark environments. It's also great for people who prefer darker colors." />
</ThemeContainer>
</ThemeContext.Provider>

<ThemeContext.Provider value={"hacker"}>
<ThemeContainer
title="Hacker theme"
buttonText="Use Hacker Theme"
onButtonClick={(x) => setTheme("hacker")}
>
<img
width={50}
height={50}
src={process.env.PUBLIC_URL + "script.png"}
alt="random"
/>
<ThemedText text="Hackers gonna hack, hack, hack..." />
</ThemeContainer>
</ThemeContext.Provider>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can see how each component has its own provider with a hardcoded value. This will override the value for this particular part of the page. This enable us to define different values in different parts of our page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant