parent
88dacd93b1
commit
6ad2cb97ae
@ -0,0 +1,24 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Conditional Rendering</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.jsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "conditional-rendering",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"react": "^18.0.0",
|
||||||
|
"react-dom": "^18.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "^18.0.0",
|
||||||
|
"@types/react-dom": "^18.0.0",
|
||||||
|
"@vitejs/plugin-react": "^1.3.0",
|
||||||
|
"vite": "^2.9.9"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
.App {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-logo {
|
||||||
|
height: 40vmin;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: no-preference) {
|
||||||
|
.App-logo {
|
||||||
|
animation: App-logo-spin infinite 20s linear;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-header {
|
||||||
|
background-color: #282c34;
|
||||||
|
min-height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-size: calc(10px + 2vmin);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-link {
|
||||||
|
color: #61dafb;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes App-logo-spin {
|
||||||
|
from {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
font-size: calc(10px + 2vmin);
|
||||||
|
}
|
@ -0,0 +1,221 @@
|
|||||||
|
// index.js
|
||||||
|
import React from 'react'
|
||||||
|
import ReactDOM from 'react-dom'
|
||||||
|
|
||||||
|
// class based component
|
||||||
|
class Header extends React.Component {
|
||||||
|
render() {
|
||||||
|
console.log(this.props.data)
|
||||||
|
const {
|
||||||
|
welcome,
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
author: { firstName, lastName },
|
||||||
|
date,
|
||||||
|
} = this.props.data
|
||||||
|
|
||||||
|
return (
|
||||||
|
<header style={this.props.styles}>
|
||||||
|
<div className='header-wrapper'>
|
||||||
|
<h1>{welcome}</h1>
|
||||||
|
<h2>{title}</h2>
|
||||||
|
<h3>{subtitle}</h3>
|
||||||
|
<p>
|
||||||
|
{firstName} {lastName}
|
||||||
|
</p>
|
||||||
|
<small>{date}</small>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Message = ({ message }) => (
|
||||||
|
<div>
|
||||||
|
<h1>{message}</h1>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
const Login = () => (
|
||||||
|
<div>
|
||||||
|
<h3>Please Login</h3>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
const Welcome = (props) => (
|
||||||
|
<div>
|
||||||
|
<h1>Welcome to 30 Days Of React</h1>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
// A button component
|
||||||
|
const Button = ({ text, onClick, style }) => (
|
||||||
|
<button style={style} onClick={onClick}>
|
||||||
|
{text}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
|
||||||
|
// TechList Component
|
||||||
|
// class base component
|
||||||
|
class TechList extends React.Component {
|
||||||
|
render() {
|
||||||
|
const { techs } = this.props
|
||||||
|
const techsFormatted = techs.map((tech) => <li key={tech}>{tech}</li>)
|
||||||
|
return techsFormatted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main Component
|
||||||
|
// Class Component
|
||||||
|
class Main extends React.Component {
|
||||||
|
render() {
|
||||||
|
const {
|
||||||
|
techs,
|
||||||
|
greetPeople,
|
||||||
|
handleTime,
|
||||||
|
loggedIn,
|
||||||
|
handleLogin,
|
||||||
|
message,
|
||||||
|
} = this.props
|
||||||
|
console.log(message)
|
||||||
|
|
||||||
|
const status = loggedIn ? <Welcome /> : <Login />
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<div className='main-wrapper'>
|
||||||
|
<p>Prerequisite to get started react.js:</p>
|
||||||
|
<ul>
|
||||||
|
<TechList techs={this.props.techs} />
|
||||||
|
</ul>
|
||||||
|
{techs.length === 3 && (
|
||||||
|
<p>You have all the prerequisite courses to get started React</p>
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
<Button
|
||||||
|
text='Show Time'
|
||||||
|
onClick={handleTime}
|
||||||
|
style={buttonStyles}
|
||||||
|
/>{' '}
|
||||||
|
<Button
|
||||||
|
text='Greet People'
|
||||||
|
onClick={greetPeople}
|
||||||
|
style={buttonStyles}
|
||||||
|
/>
|
||||||
|
{!loggedIn && <p>Please login to access more information about 30 Days Of React challenge</p>}
|
||||||
|
</div>
|
||||||
|
<div style={{ margin: 30 }}>
|
||||||
|
<Button
|
||||||
|
text={loggedIn ? 'Logout' : 'Login'}
|
||||||
|
style={buttonStyles}
|
||||||
|
onClick={handleLogin}
|
||||||
|
/>
|
||||||
|
<br />
|
||||||
|
{status}
|
||||||
|
</div>
|
||||||
|
<Message message={message} />
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CSS styles in JavaScript Object
|
||||||
|
const buttonStyles = {
|
||||||
|
backgroundColor: '#61dbfb',
|
||||||
|
padding: 10,
|
||||||
|
border: 'none',
|
||||||
|
borderRadius: 5,
|
||||||
|
margin: '3px auto',
|
||||||
|
cursor: 'pointer',
|
||||||
|
fontSize: 22,
|
||||||
|
color: 'white',
|
||||||
|
}
|
||||||
|
|
||||||
|
// Footer Component
|
||||||
|
// Class component
|
||||||
|
class Footer extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props)
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<footer>
|
||||||
|
<div className='footer-wrapper'>
|
||||||
|
<p>Copyright {this.props.date.getFullYear()}</p>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class App extends React.Component {
|
||||||
|
state = {
|
||||||
|
loggedIn: false,
|
||||||
|
techs: ['HTML', 'CSS', 'JS'],
|
||||||
|
message: 'Click show time or Greet people to change me',
|
||||||
|
}
|
||||||
|
handleLogin = () => {
|
||||||
|
this.setState({
|
||||||
|
loggedIn: !this.state.loggedIn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
showDate = (time) => {
|
||||||
|
const months = [
|
||||||
|
'January',
|
||||||
|
'February',
|
||||||
|
'March',
|
||||||
|
'April',
|
||||||
|
'May',
|
||||||
|
'June',
|
||||||
|
'July',
|
||||||
|
'August',
|
||||||
|
'September',
|
||||||
|
'October',
|
||||||
|
'November',
|
||||||
|
'December',
|
||||||
|
]
|
||||||
|
|
||||||
|
const month = months[time.getMonth()].slice(0, 3)
|
||||||
|
const year = time.getFullYear()
|
||||||
|
const date = time.getDate()
|
||||||
|
return `${month} ${date}, ${year}`
|
||||||
|
}
|
||||||
|
handleTime = () => {
|
||||||
|
let message = this.showDate(new Date())
|
||||||
|
this.setState({ message })
|
||||||
|
}
|
||||||
|
greetPeople = () => {
|
||||||
|
let message = 'Welcome to 30 Days Of React Challenge, 2020'
|
||||||
|
this.setState({ message })
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const data = {
|
||||||
|
welcome: '30 Days Of React',
|
||||||
|
title: 'Getting Started React',
|
||||||
|
subtitle: 'JavaScript Library',
|
||||||
|
author: {
|
||||||
|
firstName: 'Asabeneh',
|
||||||
|
lastName: 'Yetayeh',
|
||||||
|
},
|
||||||
|
date: 'Oct 9, 2020',
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='app'>
|
||||||
|
<Header data={data} />
|
||||||
|
|
||||||
|
<Main
|
||||||
|
techs={techs}
|
||||||
|
handleTime={this.handleTime}
|
||||||
|
greetPeople={this.greetPeople}
|
||||||
|
loggedIn={this.state.loggedIn}
|
||||||
|
handleLogin={this.handleLogin}
|
||||||
|
message={this.state.message}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Footer date={new Date()} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
@ -0,0 +1,13 @@
|
|||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||||
|
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||||
|
sans-serif;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||||
|
monospace;
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import ReactDOM from 'react-dom/client'
|
||||||
|
import App from './App'
|
||||||
|
import './index.css'
|
||||||
|
|
||||||
|
ReactDOM.createRoot(document.getElementById('root')).render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<App />
|
||||||
|
</React.StrictMode>
|
||||||
|
)
|
@ -0,0 +1,7 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import react from '@vitejs/plugin-react'
|
||||||
|
|
||||||
|
// https://vitejs.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [react()]
|
||||||
|
})
|
Loading…
Reference in new issue