Coder Social home page Coder Social logo

tyre's Introduction

tyre

A port of ReasonReact to Typescript

What

A very straightforward copy-paste of ReasonReact:

  • Look into docs
  • Understand what they say
  • Look into Reason code
  • Look into generated JavaScrip code
  • Port (or copy-paste generated JS)
  • Fix bugs
  • Make sure it works
  • ???
  • Fun (no profit)

How

Everything in the docs up to and including "Lifecycles" should work.

In Reason

type action =
  | Click
  | Toggle;

type state = {count: int, show: bool};

let component = ReasonReact.reducerComponent "MyForm";

let make _children => {
  ...component,
  initialState: fun () => {count: 0, show: false},
  reducer: fun action state =>
    switch action {
    | Click => ReasonReact.Update {...state, count: state.count + 1}
    | Toggle => ReasonReact.Update {...state, show: not state.show}
    },
  render: fun self => {
    let message = "Clicked " ^ string_of_int self.state.count ^ " times(s)";
    <div>
      <MyDialog
        onClick=(self.reduce (fun _event => Click))
        onSubmit=(self.reduce (fun _event => Toggle)) />
      (ReasonReact.stringToElement message)
    </div>
  }
};

In Typescript

import {Tyre} from './TyreReact'
import createReducerComponent = Tyre.createReducerComponent

enum Action {
    Click = 0,
    Toggle
}

type State = {count: number, show: boolean}

const Component = createReducerComponent<null, State, Action>('MyForm', {
    initialState: () => {
        return {count: 0, show: false}
    },
    reducer: (action : Action, state : State) => {
        switch (action) {
            case Action.Click:
                return Tyre.Update({...state, count: state.count + 1})
            case Action.Toggle:
                return Tyre.Update({...state, show: !state.show})
        }
    },
    render: (self) => {
        const message = `Clicked ${self.state.count} times(s)`
        return <div>
            <MyDialog
                onClick={self.reduce ((_event) => Action.Click)}
                onSubmit={self.reduce ((_event) => Action.Toggle)} />
            message
        </div>
    }
})

tyre's People

Contributors

dmitriid avatar

Watchers

James Cloos avatar Josh Girvin avatar Matthew Wilton avatar David Wilson 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.