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-apppulp init --forcepulp buildsrc/Main.js
varReact=require("react");varRN=require("react-native");exports.text=function(props){returnfunction(str){returnReact.createElement(RN.Text,props,str);};};moduleMainwhereforeignimportdataReactElement::Typeforeignimport text ::forallprops.props->String->ReactElementmain::ReactElement
main = text { style : { color :"green", fontSize :50 } } "Hello from PureScript!"importReactfrom'react';importMainfrom"./output/Main";exportdefaultclassAppextendsReact.Component{render(){returnMain.main;}}pulp buildyarn start (npm may also work)
1. React with PureScript
Clone create-react-app-purescript. Run
npm installoryarn install.Step by step instructions:
create-react-appconfiguration.craco-purescript-loader.package.jsonand install dependencies withnpm install{ "postinstall": "spago build --deps-only" }react-basicandreact-basic-hooks.Counter.ps.purescript-module.d.ts.App.tsx.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 offp-ts.Some examples:
purescript-react-realworld: A real world implementation of Conduit.gatsby-purescript-example: Simple example app using Gatsby.js with PureScript.next-purescript-example: Simple example app using Next.js with PureScript.2. React Native with PureScript
create-react-native-app purescript-apppulp init --forcepulp buildsrc/Main.jssrc/Main.purs./App.jspulp buildyarn start(npm may also work)