GithubHelp home page GithubHelp logo

basuabhirup / react-components Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 8 KB

Created with CodeSandbox

Home Page: https://codesandbox.io/s/github/basuabhirup/react-components

HTML 19.90% CSS 3.36% JavaScript 76.74%

react-components's Introduction

React Components and Styling:

Code Sandbox Live Site URL: https://codesandbox.io/s/github/basuabhirup/react-components

Project Screentshot

Objective of this Project:

  • To undesrtand JSX and Babel
  • To use Javascript Expressions in JSX
  • To understand JSX attributes and inline styling
  • To create a React app from scratch that displays a dynamic greetings message, based on the time of the day (it displays "Good Morning" if between midnight and 12PM, "Good Afternoon" if between 12PM and 6PM and "Good Night" if between 6PM and midnight)
  • To dynamically change the color of the greetings message, based on the time of the day (Morning = red, Afternoon = green, Night = blue)
  • To split the main codebase into sevral components for modualrity and reusability

Steps I have followed:

  1. Installed react and react-dom dependencies and imported them inside the main index.js file:
import React from "react";
import ReactDOM from "react-dom";

  1. Created logic inside index.js file to get the browser's current time and accordingly change the value of two custom variables - greeting and customStyle:
const date = new Date();
const currentTime = date.getHours();

let greeting;
const customStyle = {
  color: ""
};

if (currentTime < 12) {
  greeting = "Good Morning";
  customStyle.color = "red";
} else if (currentTime < 18) {
  greeting = "Good Afternoon";
  customStyle.color = "green";
} else {
  greeting = "Good Night";
  customStyle.color = "blue";
}

  1. Rendered the JSX file containing these variables inside the parent div with id root:
ReactDOM.render(
  <h1 className="heading" style={customStyle}>
    {greeting}
  </h1>,
  document.getElementById("root")
);

  1. Finally splitted the main code in index.js into two components - App and Heading for reusability, modularity and a better readability:
// Final Code inside 'index.js' file:
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";

ReactDOM.render(<App />, document.getElementById("root"));
// Final Code inside 'components/App.jsx' file:
import React from "react";
import Heading from "./Heading";

function App() {
  return (
    <div>
      <Heading />
    </div>
  );
}

export default App;
// Final Code inside 'components/Heading.jsx' file:
import React from "react";

function Heading() {
  const date = new Date();
  const currentTime = date.getHours();

  let greeting;
  const customStyle = {
    color: ""
  };

  if (currentTime < 12) {
    greeting = "Good Morning";
    customStyle.color = "red";
  } else if (currentTime < 18) {
    greeting = "Good Afternoon";
    customStyle.color = "green";
  } else {
    greeting = "Good Night";
    customStyle.color = "blue";
  }

  return (
    <h1 className="heading" style={customStyle}>
      {greeting}
    </h1>
  );
}

export default Heading;

react-components's People

Contributors

basuabhirup avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.