Skip to content

PureScript 与 React #3

Description

@lambda-forum

1. React with PureScript

  • Clone create-react-app-purescript. Run npm install or yarn install.

  • Step by step instructions:

    • Create react application with TypeScript.
    npx create-react-app create-react-app-purescript --template typescript
    • Follow the craco guide to overwrite parts of the create-react-app configuration.
    • Extend craco configuration to use craco-purescript-loader.
    • Install PureScript (compiler) and initialize spago (package manager).
    npm install purescript spago --save-dev
    npx spago init
    • Add npm script in package.json and install dependencies with npm install
    {
    "postinstall": "spago build --deps-only"
    }
    npx spago install react-basic react-basic-dom react-basic-hooks
    • Add a PureScript component: Counter.ps.
    moduleCounterwhereimportPreludeimportEffect (Effect)
    importEffect.Unsafe (unsafePerformEffect)
    importReact.Basic.DOMasRimportReact.Basic.Events (handler_)
    importReact.Basic.HooksimportReact.Basic.HooksasReactmkCounter::ReactComponent {}
    mkCounter = unsafePerformEffect counter
    counter::Effect (ReactComponent {})
    counter =do
    reactComponent "Counter"\_ ->React.do
    count /\ setCount <- useState 0let
    handleClick = handler_ <<< setCount
    pure$R.div_
    [ R.button { onClick: handleClick (_ -1), children: [ R.text "-" ] }
    , R.div_ [ R.text (show count) ]
    , R.button { onClick: handleClick (_ +1), children: [ R.text "+" ] }
    ]
    • Allow module import in TS: purescript-module.d.ts.
    • Import the component and use it: App.tsx.
    import{mkCounterasCounter}from"./Counter.purs";// ...functionApp(){return<Counter/>;}
  • purescript-tsd-gen: TypeScript Declaration File (.d.ts) generator for PureScript. It helps to use PureScript modules from TypeScript. However it does not support higher-kinded types currently because emulating HKT in TypeScript is a little bit complicated. But maybe we can have a look at the implementation of fp-ts.

  • Some examples:

2. React Native with PureScript

  • create-react-native-app purescript-app
  • pulp init --force
  • pulp build
  • src/Main.js
varReact=require("react");varRN=require("react-native");exports.text=function(props){returnfunction(str){returnReact.createElement(RN.Text,props,str);};};
  • src/Main.purs
moduleMainwhereforeignimportdataReactElement::Typeforeignimport text ::forallprops.props->String->ReactElementmain::ReactElement
main = text { style : { color :"green", fontSize :50 } } "Hello from PureScript!"
  • ./App.js
importReactfrom'react';importMainfrom"./output/Main";exportdefaultclassAppextendsReact.Component{render(){returnMain.main;}}
  • pulp build
  • yarn start (npm may also work)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions