npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-fp-ts

v2.1.0

Published

A simple functional style API for React designed for TypeScript

Downloads

9

Readme

react-fp-ts

A simple functional style React API for TypeScript.

This project started as a fork of purescript-react-basic but has been rewritten completely in TypeScript. The inspiration behind both is Reason React.

Installation

npm install react-fp-ts

Example

import * as React from 'react'
import { Self, _capture, reducerComponent, make, update } from 'react-fp-ts'

type State = number
type Action = { type: 'increment' }

const increment = { type: 'increment' }

const component = reducerComponent('Counter')

export const Counter = make(component, {
  initialState: 0,

  reducer: (self, action) => {
    switch (action.type) {
      case 'increment': return update(self.state + 1)
    }
  },

  render: (self: Self<{}, State, Action>) =>
    <div>
      <span>{self.state}</span>
      <button onClick={_capture(self, increment)}>Click</button>
    </div>
})

Writing a component

You write a component by first creating an empty component with a new identity and a chosen name which is used in debugging as the component's displayName:

const component = reducerComponent('Counter')

Note that this is an effectful operation – it creates a new component type each time it is called.

make can then be called to provide the implementation for the component:

make<P, S, A>(component: ReducerComponent<P>, spec: ComponentSpec<P, S, A>): (props: P) => JSX

It is passed the newly created component type and the spec of type ComponentSpec<P, S, A>. The type arguments are for props, state and the action respectively. A spec is an object with keys of the following type:

  • initialState: S: the initial state of the component

  • render: (self: Self<P, S, A>) => JSX,: a function from Self to JSX. This is your normal React render function with an explicit Self argument.

  • reducer: (self: Self<P, S, A>, action: A) => StateUpdate<P, S, A>,: A function from Self and action to some StateUpdate. This is where you put all your logic. The reducer should be a pure function, yet you can still do (and should do) side effects by wrapping them in a StateUpdate. A SteteUpdate can be one of:

    • noUpdate: StateUpdate<P, S, A>: a no-op.
    • update<P, S, A>(state: S): StateUpdate<P, S, A>: a simple state update.
    • updateAndSideEffects<P, S, A>(state: S, fn: (self: Self<P, S, A>) => void): StateUpdate<P, S, A>: update the state and then do a side effect.
    • sideEffects<P, S, A>(fn: (self: Self<P, S, A>) => void): StateUpdate<P, S, A>: perform some side-effects wrapped in function which receives the Self as argument.
  • didMount?: (self: Self<P, S, A>) => void: An optional key that corresponds to react's componentDidMount life-cycle hook.

  • didUpdate?: (self: Self<P, S, A>, prevProps: Readonly<P>, prevState: Readonly<S>) => void An optional key that corresponds to react's componentDidUpdate life-cycle hook.

  • willUnmount?: (self: Self<P, S, A>) => void: An optional key that corresponds to react's componentWillUnmount life-cycle hook.

Why should I use this?

  • You want a simpler API to React with more focus on functional programming.
  • Have a single place to put all state changes and the domain logic (see reducer in ComponentSpec).
  • Avoid using this and pass self around explicitly, which lends itself to easier abstraction and better reasoning.
  • No other dependencies than react.

Acknowledgements

  • Elm
  • ReasonReact
  • purescript-react-basic