Giter Club home page Giter Club logo

my-own-react-lib's Introduction

React.js Clone

Motivation

  • For light weight lib like react for vanilla js project
  • To confirm the react lifecycle

Topics

  • set up project with webpack and babel
  • jsx transforming with babel plugin
  • config prettier and eslint
  • built react-like lib from scratch
  • support useState, useEffect, function component
  • webpack dev server

How to use this library

  1. Download or clone this repo
https://github.com/FrozenIce0617/my-own-react-lib 
  1. Install dependencies
yarn or yarn install

Now, you can use MyOwnReact as like react in .src/index.js file.

// import library
import MyOwnReact from '../lib/MyOwnReact'
import App from './App'

MyOwnReact.render(<App />, document.getElementById('root'))

we can create App in ./App.js file like react component

import MyOwnReact from '../lib/MyOwnReact'
const App = () => {
    return <h1>Hello world </h1>
}

export default App

you can run npx webpack serve as dev server, then you can see the app is running at localhost:9000 on browser.

demo app

The demo app shows the dad joke and possible to see next or prev joke by clicking buttons. It is implemented with useEffect, useState of this lib.

screenshot

import MyOwnReact from '../lib/MyOwnReact'
require('./style.css')

const DAD_JOKE_BASE_URL = 'https://icanhazdadjoke.com'
const MAX_INDEX = 10

const App = () => {
  const [index, setIndex] = MyOwnReact.useState(0)
  const [jokes, setJokes] = MyOwnReact.useState([])

  const handleEvent = (isNext = true) => {
    const nextIndex = isNext ? index + 1 : index - 1
    setIndex((nextIndex + MAX_INDEX) % MAX_INDEX)
  }

  MyOwnReact.useEffect(() => {
    // fetching joke list using api at first loading...
    fetch(`${DAD_JOKE_BASE_URL}/search`, {
      headers: {
        accept: 'application/json',
      },
    })
      .then((response) => response.json())
      .then((jsonData) => {
        setJokes(jsonData.results)
      })
  }, [])

  return (
    <div className="App">
      <div className="content">
        <div className="controls">
          <button onClick={() => handleEvent(false)} className="btn btn-prev">
            Prev
          </button>
          <h2>Joke {index + 1}</h2>
          <button onClick={() => handleEvent()} className="btn btn-next">
            Next
          </button>
        </div>
        <h3 className="joke">{jokes[index]?.joke}</h3>
      </div>
    </div>
  )
}

export default App

my-own-react-lib's People

Contributors

frozenice0617 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  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.