Coder Social home page Coder Social logo

think-with-react's Introduction

Logo-nav

Kalvium Lab | REACT - Think with React

Learning Goals

In this exercise, the goal is to apply as many as possible of the concepts you've just learned:

  • when and how to setup react in your application,
  • ways of using react.

Getting started

  1. Fork this repo
  2. Clone this repo

Whenever you create a first significant change, you should make your first commit.

  1. Follow these guidelines to add, commit and push changes.

Introduction

In this exercise, you will try to setup the react application. As you can see react is a javascript library to create super cool reusable UI. Our goal is to understand the ways in which you can include react to your application.

We will divide our work into three parts:

  • part I - Include react library in your html file

  • part II - Use JSX and babel compiler

  • Part III - CREATE REACT APP

So let's get started!

part I

REACT 1 | LET'S START UP

Your task in this iteration is just to finish the react setup. To do this, let's use the same html file what we created and create a div with an id called react container. You have included the react libraries to your javascript file. But we have to include our react element to your index.html. To do this create a file called app.js and include it in your html file.

<body>
    <!-- Create a div to setup a react container -->
   <div id="react-container"></div>
 
    <!-- Load the React Library  -->
    <!-- React and React DOM -->
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
   
   <!-- Include the react element -->
    <script src="./app.js"></script>
</body>

You're ready to move to the next iteration. πŸ™Œ

REACT 2 | CHECK N CHECK

Your task in this iteration is to check whether you have successfully setup react in your application. To do it inside the app.js, get the div tag like how you get the element using it's id.

const container = document.getElementById('react-container');

Now the most important step, How to make a difference between Javascript and React. ReactDOM is the place where you render the output. Now how to render using ReactDOM. Kindly look into the code snippet given below.

ReactDOM.render("Hello! Welcome to React",container);

If you do the above, you need to get an output similar to this. Image description

If you get the above output, you can ensure that the react is completely setup in your system.

REACT 3 | IS IT A FUNCTION

The component can be a functional component or class component. Now before moving to the class component let us try to use the same example given above and try to convert it as a functional component. This is going to be our output. Image description

To get the above it is quite simple. Check the code snippet given below. There is no modification required in the index.html. React.createElement has three attributes (component,props,children) Component can be a react component - reusable piece of UI. Props are properties of the component or element. children can be a nested component or html element.

In the below code when you check the react element it has a div tag followed by null and a string called Hey Kalvians! Welcome to React Learning. The div tag refers to the component. null refers to the property and the statement is the children. When you inspect the element you can see a output similar to this. The next div tag is the children. Image description

// Functional component
const Container = () =>{
    return React.createElement(`div`,null,`Hey Kalvians! Welcome to React Learning`,
       React.createElement(`div`,null,`Let's rock and roll`)
       );
}
// Render the Container
const container = document.getElementById('react-container');
ReactDOM.render(React.createElement(Container),container);

REACT 4 | NOW IT'S CLASS TIME

Now in this iteration we will try working with the class component. To define a class component. We are going to replicate the same output what we got in function component. Check the code snippet below. Replace the above code instead of functional components and you can see that it should print

Hey Kalvians Let's rock and roll

class ReactContainer extends React.Component{
  // constructor - to initialize the state 

  // render method to render the react dom 
    render(){
        return React.createElement(`div`,null,`Hey Kalvians`,
        React.createElement(`div`,null,`Let's rock and roll with classes`)
        );
    }
  
}
const container = document.getElementById('react-container');
ReactDOM.render(React.createElement(ReactContainer),container);

You're ready to move to the next iteration. πŸ™Œ

part II - Use JSX and babel compiler

In the progressions above, we only relied on features that are natively supported by the browsers. This is why we used a JavaScript function call to tell React what to display: However, React also offers an option to use JSX instead.JSX stands for Javascript XML,

So far whatever we have used we did it directly without the use of Babel compiler. Babel is a very powerful javascript complier which converts react script to Browser acceptable Javascript. As you have learnt that react uses virtual dom and babel helps to transpile to browser javascript.

The quickest way to try JSX in your project is to add this <script> tag to your page:

    <!-- Include babel compiler -->
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

Now you can use JSX in any <script> tag by adding type="text/babel" attribute to it.

    class ReactContainer extends React.Component{
    // JSX tags
    // JSX there should be one parent element
      render(){
          return (
          <div>Hello! Welcome to Kalvium   
             <div>This is babel</div>
         </div> )
      };  
  }

const container = document.getElementById('react-container');
// Direclty pass the component to render
ReactDOM.render(<ReactContainer/>,container);    

Your output should look something like this:

Isn't the above one so easy. So instead of too many React.createElement you can use JSX which is super cool to use.

Part III - CREATE REACT APP

This is a project aimed at getting you up to speed with React in no time. It provides a ready-made React application starter, so you can dive into building your app without having to deal with Webpack and Babel configurations.

It provides out of the box:

I. A development server with hot reloading

II. Provides a testing environment with Jest

III. Allows to build the React app

IV. Ready for ES6+ syntax

V. Bundles all your JavaScript and assets

VI. Comes with CSS autoprefixer, SASS and CSS Modules support

When you run npm create vite@latest <app-name>, npm is going to download the most recent release, run it, and then remove it from your system. This is great because you will never have an outdated version on your system, and every time you run it, you’re getting the latest and greatest code available.

To create the app :

npm create vite@latest <your-react-app-name>

Then choose react framework and then javascript option and enter.

To run it open the console and go to the application directory

cd your-react-app-name 

, and just type

npm run dev 

Your task is to display hello Kalvium --> using this method.

How to submit

For each part - create a new repository and share your GitHub links as instructed in assignment.

Summary

In this exercise, you've learnt various ways of setting up react in your system. If you managed to do it, good job! πŸ†

This concludes the React setup. We are proud of you!

Happy Coding ❀️!

think-with-react's People

Contributors

kalvium-program 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.