forked from koretskiyav/react-2020-11-13
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01dbe1c
commit cef9805
Showing
5 changed files
with
47 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import React, { PureComponent } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { Route, Switch } from 'react-router-dom'; | ||
import Header from '../header'; | ||
import Basket from '../basket'; | ||
import RestaurantsPage from '../../pages/restaurants-page'; | ||
import { UserProvider } from '../../contexts/user-context'; | ||
|
||
export default class App extends PureComponent { | ||
render() { | ||
return ( | ||
<div> | ||
const App = () => { | ||
const [name, setName] = useState('Igor'); | ||
return ( | ||
<div> | ||
<UserProvider value={{ name, setName }}> | ||
<Header /> | ||
<Switch> | ||
<Route path="/checkout" component={Basket} /> | ||
<Route path="/restaurants" component={RestaurantsPage} /> | ||
<Route path="/error" component={() => <h1>Error Page</h1>} /> | ||
<Route path="/" component={() => '404 - not found'} /> | ||
</Switch> | ||
</div> | ||
); | ||
} | ||
} | ||
</UserProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import React from 'react'; | ||
import React, { useContext } from 'react'; | ||
|
||
import Logo from './logo'; | ||
import styles from './header.module.css'; | ||
import userContext from '../../contexts/user-context'; | ||
|
||
const Header = () => ( | ||
<header className={styles.header}> | ||
<Logo /> | ||
</header> | ||
); | ||
const Header = () => { | ||
const { name, setName } = useContext(userContext); | ||
|
||
return ( | ||
<header className={styles.header} onClick={() => setName('Ivan')}> | ||
<Logo /> | ||
<h2>{name}</h2> | ||
</header> | ||
); | ||
}; | ||
export default Header; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createContext } from 'react'; | ||
|
||
const userContext = createContext({ name: 'default name' }); | ||
|
||
export const UserConsumer = userContext.Consumer; | ||
export const UserProvider = userContext.Provider; | ||
|
||
export default userContext; |