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

Completed Creating and Updating Context exercise #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
almost complete issue with age not showing up
lawsonjerry committed Jan 3, 2024
commit 6d0a8aabd3f70e0526b163bdffad0eb3f2c05bea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ Learn to create and dynamically update a Context in React. Understand how change

4. **Initial Setup**:
- Change to the exercise directory: `cd 03-26-creating-updating-context`.
- Run `npm install` to install dependencies.
- Run `npm install` to install dependencies

## Exercise Instructions

40 changes: 22 additions & 18 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import logo from './logo.svg';
import './App.css';
import React, { useState } from 'react';
import UserContext from './UserContext';
import UserProfile from './UserProfile';

function App() {
const [user, setUser] = useState({
name: 'John Doe',
age: 30,
});


// const [isOriginalName, setIsOriginalName] = useState(true)

const handleChangeName = () => {
// setUser({ ...user, name: isOriginalName ? 'Jane Doe' : 'John Doe' });
setUser({ ...user, name: 'Jane Doe' });
// setIsOriginalName(!isOriginalName);
};

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
// <UserContext.Provider value={{ user, isOriginalName }}>
<UserContext.Provider value={user}>
<UserProfile />
<button onClick={handleChangeName}>Change Name</button>
</UserContext.Provider>
);
}

8 changes: 8 additions & 0 deletions src/UserContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React, { createContext } from 'react';

const UserContext = React.createContext({
name: 'John Doe',
age: 30
});

export default UserContext
21 changes: 21 additions & 0 deletions src/UserProfile.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { useContext } from 'react';
import UserContext from './UserContext';

const UserProfile = () => {
const { user } = useContext(UserContext);


// const isOriginalName = useContext(UserContext).isOriginalName;


return (
<div>
<h2>User Profile</h2>
{/* <p>Name: {isOriginalName ? 'John Doe' : 'Jane Doe' }</p> */}
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
</div>
);
};

export default UserProfile;