GithubHelp home page GithubHelp logo

justinwoo / actually-typing-your-react-redux-program Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 1.0 1 KB

A guide for those who are stuck not being able to migrate their apps away from JS/TypeScript/Flow. See index.tsx

TypeScript 100.00%

actually-typing-your-react-redux-program's Introduction

actually-typing-your-react-redux-program's People

Contributors

justinwoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

romanlex

actually-typing-your-react-redux-program's Issues

What about?

import { connect } from 'react-redux'

type Dispatch<A> = (a: A) => void

function myconnect<S, A>(): <OP>() => <SP, DP>(
  f: (state: S, ownProps: OP) => SP,
  g: (dispatch: Dispatch<A>, ownProps: OP) => DP
) => (c: React.ComponentType<OP & SP & DP>) => React.ComponentClass<OP> {
  return () => (f, g) => (connect as any)(f, g)
}

Setup

type State = {
  count: number
}

type Action = { type: 'INCREMENT' } | { type: 'DECREMENT' }

const appConnect = myconnect<State, Action>()

Usage

import * as React from 'react'

class Foo extends React.Component<{ message: string; inc: () => void; dec: () => void; style: object }> {}

// ConnectedFoo: React.ComponentClass<{ style: object }>
const ConnectedFoo = appConnect<{ style: object }>()(
  state => ({
    message: `Clicks: ${state.count}`
  }),
  dispatch => ({
    inc: () => dispatch({ type: 'INCREMENT' }),
    dec: () => dispatch({ type: 'DECREMENT' })
  })
)(Foo)

It looks equivalent but less verbose

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.