All Projects β†’ vutran1710 β†’ WriteYourOwnReact

vutran1710 / WriteYourOwnReact

Licence: other
Write your own version of React. Why? Because you CAN!

Programming Languages

typescript
32286 projects
CSS
56736 projects

Projects that are alternatives of or similar to WriteYourOwnReact

python-tutorial-codes
Python 🐍 Tutorials
Stars: ✭ 23 (+15%)
Mutual labels:  diy, diycode
mcp3008.js
A node.js module for querying an mcp3008 analog/digital converter.
Stars: ✭ 24 (+20%)
Mutual labels:  diy, diycode
firmware
Alternative IP Camera firmware from an open community
Stars: ✭ 236 (+1080%)
Mutual labels:  diy
pedalevite
PΓ©dale Vite β€” DIY multi-FX pedalboard for guitar/bass/etc.
Stars: ✭ 68 (+240%)
Mutual labels:  diy
arktur
No description or website provided.
Stars: ✭ 38 (+90%)
Mutual labels:  fun
DossyTextLabel
A subclass of UILabel reminiscent of Strong Bad's faithful Tandy 400.
Stars: ✭ 75 (+275%)
Mutual labels:  fun
Laser-XY-Scanner
Low Cost DIY Laser XY Scanner, Cutter, or Engraver
Stars: ✭ 27 (+35%)
Mutual labels:  diy
weather-station
Everything you need to run and monitor your own open source weather station. β›…
Stars: ✭ 38 (+90%)
Mutual labels:  diy
pcb-ws2812-wifi-controller
WiFi Controller for WS2812 LED Stripes
Stars: ✭ 48 (+140%)
Mutual labels:  diy
ATtiny13-TinySolder
T12 Quick Heating Soldering Station
Stars: ✭ 45 (+125%)
Mutual labels:  diy
antispy-jammer
Simplest ultrasonic ANTISPY voice recording jammer based on ATTINY13 / ATTINY85 / ARDUINO with PAM8403 module driving piezo ultrasonic transducers (and optionally AD8933 signal generator)
Stars: ✭ 39 (+95%)
Mutual labels:  diy
SHA3ImplementedInsideofaRARfile
The Keccak hash algorithm implemented inside of a RAR archive using the RAR filter assembly language
Stars: ✭ 21 (+5%)
Mutual labels:  fun
A-for-apple
No description or website provided.
Stars: ✭ 14 (-30%)
Mutual labels:  fun
penny
3 servos, 10 dollars hexapod
Stars: ✭ 26 (+30%)
Mutual labels:  fun
ATtiny85-TinyCharger
Single-Cell Li-Ion Battery Charger with Monitoring
Stars: ✭ 20 (+0%)
Mutual labels:  diy
Play-Notes
A simple Powershell function that lets you write/play songs in musical notation using the Beep() function in windows.
Stars: ✭ 17 (-15%)
Mutual labels:  fun
random-restaurant-generator
An Android app that queries Yelp's API for a random restaurant near you
Stars: ✭ 15 (-25%)
Mutual labels:  fun
PiDSP
Home of PiDSP - a freeDSP for Raspberry Pi
Stars: ✭ 17 (-15%)
Mutual labels:  diy
PiBuilder
Ideas for building a Raspberry Pi from "bare metal" to ready-to-run IOTstack
Stars: ✭ 26 (+30%)
Mutual labels:  diy
mediaforge
A Discord bot for editing and creating videos, images, GIFs, and more!
Stars: ✭ 45 (+125%)
Mutual labels:  fun

banner

Why?

  1. You will always learn much better when you Do-It-Yourself!
  2. Fun
  3. Provide a statement: You Know React

What?

What you are having is...

  • A tiny React, with 2 basic API: createElement & RenderDOM
  • Create 2 Apps, one static and one dynamic (meaning it can update/delete)
  • Using RenderDOM mount the 2 apps to two different nodes on the DOM
  • The DOM tree after rendering would look like this:
<body>
  <!-- Static, cannot update except printing things -->
  <div id="static-app">
    <div class="example-static-app">
      This is my Static App
      <h1 class="title">This is a heading</h1>
      <div class="inner-body">
        <p class="text">Lorem ipsum dolor sit amet</p>
        <button>Say Hello</button>
      </div>
    </div>
  </div>

  <!-- Fun part - a dynamic App that can update state & props & stuffs -->
  <div id="dynamic-app">
    <div class="example-dynamic-app">
      This is my Dynamic App
      <h1>This is a dynamic counter: 1</h1>
      <button>Incr!</button>
      <button>Reset counter!</button>
      <!-- if the Counter > 7, there will be a small hint shown bellow -->
      <!-- <h4>Counter > 7 and < 10 <h4> if counter > 7 and < 10 -->
      <!-- <h5>Counter more than 10 <h5> if counter >= 10 -->
    </div>
  </div>
</body>

Architecture brief

To achieve the target mention above, the generall architecture of your React would consists of

  • A VDOM is a tree of fiber-nodes
  • Text-Node should be a fiber-node as well
  • createElement should return a fiber-node
  • after setState, render a new sub-tree of fiber-nodes and process to diffing
  • diffing algorithm is a recursive run down both trees from the stateful root-node
  • during diffing, Tree-Swap happen when a component's tag has changed, or when a component's children-structure changed
  • when structure remains the same, but attributes change then only update the corresponding nodes

Project status

  • Rendering works!
  • Updating works! (replacing tree or updating individual nodes)
  • Fiber reconciliation - not perfect, but generally it works.
  • JSX ready!
  • Class Component Life-cycle Methods (reserved for studying)
  • Better optimized diffing algorithm (reserved for studying)
  • Hooks (reserved for studying)

Learning instruction

  • You can search in project for all TODO notes to know what needs to do to further enhance your React
  • You can learn and try to implement hooks and component-life-cycle method (before-mount, did-update, memoize etc)
  • Try to optimize diffing algorithm (ref to official doc of React about reconciliation)
  • Add more supported-events for your React

Running

Screenshot

Screenshot

Using this home-made React, you can write your app something like...

const Button = (props: { onClick: EventHandler, text: string }) => {
  return <button onClick={props.onClick}>{props.text}</button>
}

const Heading = (props: { count: number }) => {
  const text = `This is a dynamic counter: ${props.count}`
  return <h1>{text}</h1>
}

const SubHeading = (props: { currentCount: number }) => {
  const { currentCount: cnt } = props

  if (cnt > 7 && cnt < 10) {
    return <h4>counter is more than 7 but less than 10, it's a H4</h4>
  }
  if (cnt >= 10) {
    return <h5>counter is more than 10, it's a H5</h5>
  }
}

class App extends React.Component {
  constructor(props: any) {
    super(props)
    this.state = {
      count: 1,
    }
  }

  handleOnClick = () => {
    this.setState({ count: this.state.count + 1 })
  }

  reset = () => {
    this.setState({ count: 0 })
  }

  render() {
    return (
      <div className="example-dynamic-app">
        <Heading count={this.state.count} />
        <Button text="Incr!" onClick={this.handleOnClick} />
        <Button text="Reset" onClick={this.reset} />
        {this.state.count > 7 && SubHeading({ currentCount: this.state.count })}
      </div>
    )
  }
}

And finally render it to a DOM node.

React.RenderDOM(App, "#container-id")
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].