Header component exercise

Header.jsx

import React, { Component } from 'react'
import logo from '../../logo.svg';

export default class Header extends Component {
    render() {
        return (
            <div>
                <img src={logo} alt="React-Logo" className="App-logo" />
                <h1 className="App-title"> Coin Exchange</h1>
            </div>
        )
    }
}

App.js

render() {
    return (
      <div className="App">
        <header className="App-header">
        <Header />
        </header>
        <AccountBalance amount={this.state.balance} />
        <CoinList coinData={this.state.coinData} />
      </div>
    )
  };

APP.js

import React from 'react';
import CoinList from './components/CoinList/CoinList';
import AccountBalance from './components/AccountBalance/AccountBalance';
import CoinExchangeHeader from './components/CoinExchangeHeader/CoinExchangeHeader'
import styled from 'styled-components'

const Div = styled.div `
  text-align: center;
  background-color: #282c34;
  color: white;
  `;


class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      balance: 10000,
      coinData: [

        {
          name: 'Bitcoin',
          ticker: 'BTC',
          price: 1000000.00
        },
        {
          name: 'Etherium',
          ticker: 'ETH',
          price: 180000.00
        },
        {
          name: 'Tether',
          ticker: 'USDT',
          price: 1.00
        },
        {
          name: 'Binance Coin',
          ticker: 'BNB',
          price: 14300.02
        },
        {
          name: 'Ripple',
          ticker: 'XRP',
          price: 15.86
        }
      ]
    }
  }

  render() {
    return (
      <Div className="App">
        <CoinExchangeHeader/>
        <AccountBalance amount={this.state.balance} />
        <CoinList coinData={this.state.coinData} />
      </Div>
    );
  } 
}

export default App;


Header Component

import React, { Component } from 'react';
import styled from 'styled-components'

const Header = styled.header`
  font-size: 20px;
  `;

export default class CoinExchangeHeader extends Component {
  render() {
      return (
          <>
              <Header>
                  <img src="logo192.png" width="100" height="100" alt='React Logo' className="App-logo" />
                  <h1>
                      Coin Exchange
                  </h1>
              </Header>
          </>
      )
  }
}


ExchangeHeader.jsx
const Header = styled.header`
    background-color: #282c34;
    min-height: 20vh;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    color: white;
`

const Img = styled.img`
    height: 8rem;
    pointer-events: none;
`

const H1 = styled.h1`
    font-size: 36px;
`


    return (
        <Header>
        <Img src={logo} alt="React Log" />
        <H1>
          Coin Exchange
        </H1>
      </Header>


App.jsx

const Div = styled.div`
  text-align: center;
  background-color: rgb(20, 56, 97);
  color: #cccccc;
`;

   return (
      <Div>   
        <ExchangeHeader  />     
        <AccountBalance amount={this.state.balance} /> 
        <CoinList coinData = {this.state.coinData} />        
      </Div>
    );
1 Like

this is my solution:

StyleTable.jsx

import styled from "styled-components"

const Thead = styled.thead`
    background: #26384D;
    background: -moz - linear - gradient(top, #5c6a79 0 %, #3b4c5e 66 %, #26384D 100 %);
    background: -webkit - linear - gradient(top, #5c6a79 0 %, #3b4c5e 66 %, #26384D 100 %);
    background: linear - gradient(to bottom, #5c6a79 0 %, #3b4c5e 66 %, #26384D 100 %);
    border - bottom: 2px solid #444444;
`
const Th = styled.th`
    table.blueTable thead th {
    font - size: 18px;
    font - weight: bold;
    color: #FFF8F4;
    text - align: center;
    border - left: 2px solid #D0E4F5;

export default (Thead, Th);

tableheader.jsx

import react from "react";
import { Thead, Th } from './StyleTable'


export default function TableHEad() {
    return (
        <thead>
            <tr>
                <th>Coin</th>
                <th>Tiker</th>
                <th>price</th>
                <th></th>
            </tr>
        </thead>
    )
}

footer.jsx

import react from "react"
export default function Tfoot() {
    return (
        <tfoot>
            <tr>
                <td colSpan="4">
                    <div className="links"><a href="#">&laquo;</a> <a href="#">&raquo;</a></div>
                </td>
            </tr>
        </tfoot>
    )
}

Table.jsx

import React, { Component } from 'react'
import Tfoot from './footer';
import TableHEad from './tableheader';
import Coin from '../coinList/coinList'




export default function Table() {
    return (

        <table className="blueTable">
            <TableHEad />
            <Tfoot />
            <tbody>
                <Coin name="Bitcoin" tiker="BTC" price={75000} />
                <Coin name="Luna" tiker="LUNA" price={200} />
                <Coin name="Avalance" tiker="AVAX" price={1000} />
            </tbody>

        </table>

    )

}

ExchangeHeader.jsx

import logo from '../../logo.svg';
import styled from 'styled-components';


const Img = styled.img`
    height: 8rem;
    pointer-events: none;
`;

const Header = styled.header`
    background-color: #282c34;
    min-height: 20vh;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex - start;
    font-size: 36px;
    color: white;
`;

const H1 = styled.h1`
    font-size: 4rem;
`;

export default class ExchangeHeader extends Component {
    render() {
        return (
            <Header>
                <Img src={logo} alt="React logo" />
                <H1>Coin Exchange</H1>
            </Header>
        )
    }
}

App.js

 render() {
   return (
     <Div>
       <ExchangeHeader />
       <AccountBalance amount={this.state.balance} />
       <CoinList coinData={this.state.coinData} />
     </Div>
   );
 }
1 Like

@Baz nice solution man. styled components are very handy arent they

1 Like
import React, { Component } from 'react'
import logo from '../../logo.svg';
import styled from 'styled-components';


const Img = styled.img`
    height: 8rem;
    pointer-events: none;
`;

const Header = styled.header`
    background-color: #282c34;
    min-height: 20vh;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex - start;
    color: white;
`;

const H1 = styled.h1`
    font-size: 4rem;
`;

export default class ExchangeHeader extends Component {
    render() {
        return (
            <Header>
                <Img src={logo} alt="React logo" />
                <H1>
                     Coin Exchange
                </H1>
            </Header>
        )
    }
}
1 Like
import React, { Component } from 'react'
import logo from '../../logo.svg';

export default class Header extends Component {
  render() {
    return (
        <header className="App-header">
        <img src={logo} alt="react logo" className='App-logo'/>
        <h1 className='App-title'>Coin Exchange</h1>
      </header>
    )
  }
}
1 Like
Header.js
import React, { Component } from 'react'
import styled from 'styled-components';
import logo from '../logo.svg';

const AppLogo = styled.img`
height: 8rem;
pointer-events: none;
`
const AppHeader = styled.header`
    background-color: #282c34;
    min-height: 20vh;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    color: white;
  `
const AppTitle = styled.h1`
    font-size: 4rem;
  `


export default class Header extends Component {


    render() {
        return (
            <AppHeader className="App-header">
                <AppLogo src={logo} alt='logo' className='App-logo'/>
                <AppTitle className='App-title'>Coin Exchange</AppTitle>
            </AppHeader>
        )
    }
1 Like
the render function, inside App.js

  render() {
    return (
      <div className="App">
        <Header />
        <AccountBalance amount={this.state.balance} />
        <CoinsList coins={this.state.coinData} />
      </div>
    );
  }
Header.js
import React, { Component } from "react";
import styled from "styled-components";

export default class Header extends Component {
  render() {
    return (
      <CoinExchangeHeader>
        <SpinningLogoImg src={logoURL} alt="" />
        <AppTitle>Coin Exchange</AppTitle>
      </CoinExchangeHeader>
    );
  }
}
const CoinExchangeHeader = styled.header`
  background-color: black;
  min-height: 40vh;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  font-size: calc(10px + 2vmin);
  color: white;
`;
const SpinningLogoImg = styled.img`
  height: 8rem;
  width: 8rem;
  pointer-events: none;
  margin: 1rem 3rem;
  animation: App-logo-spin infinite 20s linear;
`;
const AppTitle = styled.h1`
  font-size: 4rem;
`;
const logoURL =
  "https://imgs.search.brave.com/-rLo6GB3uMWXa_2Wwn_2V-zPN1WKzvbBDUs8JfIF6Iw/rs:fit:1200:1200:1/g:ce/aHR0cHM6Ly9jcnlw/dG9sb2dvcy5jYy9s/b2dvcy9zb2xhbmEt/c29sLWxvZ28ucG5n";

1 Like

Header.jsx


import React, { Component } from 'react';
import logo from './logo.svg';

export class Header extends Component {
  render() {
    return (
      <header className="App-header">
          <img src={logo} alt="React Logo" className="App-logo" />
          <h1 className="App-title">Coin Exchange</h1>
        </header>
    )
  }
}

export default Header;
1 Like
App.js
import './App.css';
import React from 'react';
import CoinList from './components/CoinList/CoinList';
import AccountBalance from'./components/AccountBalance/AccountBalance';
import Header from './components/Header/Header';

class App extends React.Component {
  constructor(props) {
    super(props);
      this.state = {
        balance: 10000,
        coinData: [
          {
            name: 'Bitcoin',
            ticker: 'BTC',
            price: 9999.99
          },
          {
            name: 'Ethereum',
            ticker: 'ETH',
            price: 299.99
          },
          {
            name: 'Tether',
            ticker: 'USDT',
            price: 1.0
          },
          {
            name: 'Ripple',
            ticker: 'XRP',
            price: 0.2
          },
          {
            name: 'Bitcoin Cash',
            ticker: 'BCH',
            price: 297.99
          },
        ]
      }
  }

    render() {
      return (
        <div className="App">
          <Header />
            <AccountBalance amount={this.state.balance} />
            <CoinList coinData={this.state.coinData} />
        </div>
      );
    }
}
export default App;
Header.jsx
import React, { Component } from 'react';
import logo from './logo.svg';
import '../AccountBalance/AccountBalance.css'

export default class Header extends Component {
  render() {
    return (
        <header className="App-header">
            <img src={logo} alt="React Logo" className="App-logo" />
            <h2 className="App-title">Coin Exchange</h2>
        </header>
    )
  }
}
CoinList.jsx
import React, { Component } from 'react'
import styled from 'styled-components'
import Coin from '../Coin/Coin';

const Table = styled.table`
  margin: 25px auto 25px auto;
  display: inline-block;
  padding: 5px;
`;

export default class CoinList extends Component {
  render() {
    return (
      <Table className>
        <thead>
            <tr>
                <th>Name</th>
                <th>Ticker</th>
                <th>Price</th>
            </tr>
        </thead>
            <tbody>
            {
            this.props.coinData.map( ({name, ticker, price}) =>
                <Coin key={ticker} name={name} ticker={ticker} price={price} />
            )
        }
        </tbody>
    </Table>
    )
  }
}
Coin.jsx
import React, { Component } from 'react'
import PropTypes from 'prop-types';
import styled from 'styled-components'

const Td = styled.td`
  border: 1px solid #cccccc;
  width: 25vh;
`;

export default class Coin extends Component {
    constructor(props) {
        super(props);
        this.state = {
            price: this.props.price
        }
        this.handleClick = this.handleClick.bind(this);
    }
    handleClick(event) {
        //prevent the default action of submitting the form
        event.preventDefault();
        const randomPercentage = 0.995 + Math.random() * 0.01;
        this.setState( function(oldState) {
            return {
                price: oldState.price * randomPercentage
            };
        });
    }

    render() {
        return (
            <tr>
                <Td>{this.props.name}</Td>
                <Td>{this.props.ticker}</Td>
                <Td>${this.state.price}</Td>
                <Td>
                    <form action="#" method="POST">
                        <button onClick={this.handleClick}>Refresh</button>
                    </form>
                </Td>
            </tr>
        );
    }
}

Coin.propTypes = {
    name: PropTypes.string.isRequired,
    ticker: PropTypes.string.isRequired,
    price: PropTypes.number.isRequired
}
AccountBalance.jsx
import React, { Component } from 'react'
import PropTypes from 'prop-types';
import './AccountBalance.css';

export default class AccountBalance extends Component {
    render() {
        return (
            <section className="AccountBalance">
                Account Balance: ${this.props.amount}
            </section>
        );
    }
}

AccountBalance.propTypes = {
    amount: PropTypes.number.isRequired
}
AccountBalance.css
.AccountBalance {
    padding-top: 1rem;
    padding-bottom: 1rem;
    border-bottom: .1rem;
    border-bottom-style: solid;
    border-bottom-color: white;
    font-weight: bold;
    background-color: rgb(12, 12, 167);
}
App.css
.App {
  text-align: center;
  background-color: rgb(0, 6, 47);
  color: white;
}

.App-logo {
  height: 4rem;
  pointer-events: none;
}

.App-header {
  background-color: #282c34;
  min-height: 10vh;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  font-size: calc(10px + 2vmin);
  color: white;
  border-bottom: .5rem;
  border-bottom-style: solid;
  border-bottom-color: #61dafb;
}

.App-title {
  font-size: 2rem;
}

.App-link {
  color: #61dafb;
}

1 Like

AppHeader.jsx

import React, { Component } from 'react'
import logo from '../../logo.svg';
import './AppHeader.css';

export default class AppHeader extends Component {

  render() {
    return (
    <header className="App-header">
        <img src={logo} alt="React logo" className='App-logo' />
        <h1 className='App-title'>
         Coin Exchange
        </h1>
    </header>
    )
  }
}

AppHeader.css

.App-header {
    background-color: #282c34;
    min-height: 20vh;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    color: white;
  }

.App-logo {
    height: 8rem;
    pointer-events: none;
  }
  
  @media (prefers-reduced-motion: no-preference) {
    .App-logo {
      animation: App-logo-spin infinite 20s linear;
    }
  }
1 Like

nicee styling g. looks great

2 Likes
import React, { Component } from "react";
import logo from './logo.svg';

export default class Header extends Component {
    render () {
        return(
            <header className="App-header">
                <img src={logo} alt='React Logo' className='App-logo'/>
                <h1 className='App-title'>
                    Coin Exchange!
                </h1>
            </header>
        );
    }
}
1 Like
import React, { Component } from 'react'
import styled from 'styled-components';
import logo from '../../logo.svg';

const Header = styled.header`
background-color: #3da4c6;
min-height: 20vh;
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
font-size: 30px;
color: rgb(244, 244, 248);
`;
This text will be hidden
const AppTitle = styled.h1`
    font-size: 4rem;
  `;

export default class Head extends Component {
  
  render() {
    return (
      <Header>
       <AppTitle>Coin Exchange 3000</AppTitle>
       <img src={logo} alt="React logo failed to load" className= "App-logo"/>
       </Header>
    )
  }
}

   }  
  }
  render () {
     return (
    <div className="App">
      <Head />
      <AccountBalance amount={this.state.balance} />
      <CoinList coinData={this.state.coinData} />
    </div>
  );
     }
    }

    export default App;This text will be hidden

App.jsx

 <Header />

Header.jsx

import React, { Component } from 'react'
import logo from './logo192.png'

export default class Header extends Component {
    render() {
        return (
            <header className="App-header">
                <img src={logo} className="App-logo" alt="logo" />
                <h1 className='app-title'>
                    Coin Exchange
                </h1>
            </header>
        )
    }
}

Hi everyone,

Took out the AppHeader to make it its own React component:
https://github.com/CodingInLondon/moralisacademy-react/commit/61c64a744e095b8d18f8d0969bfa53c0b1466495

Then introduced styled components everywhere.

Final code here:
https://github.com/CodingInLondon/moralisacademy-react/tree/90cda6190b701ae210c1786b160f593cdfa4cb76/coin-exchange

Question: in practice do people often use styled-components, does it really make code easier to maintain? :open_mouth: :question:

Cheers,
Matt