Title: PureScript 与 React · Issue #3 · lambda-forum/lambda-forum.github.io · GitHub
Open Graph Title: PureScript 与 React · Issue #3 · lambda-forum/lambda-forum.github.io
X Title: PureScript 与 React · Issue #3 · lambda-forum/lambda-forum.github.io
Description: 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 Foll...
Open Graph Description: 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-a...
X Description: 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-a...
Opengraph URL: https://github.com/lambda-forum/lambda-forum.github.io/issues/3
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PureScript 与 React","articleBody":"## 1. React with PureScript\r\n\r\n- Clone [create-react-app-purescript](https://github.com/andys8/create-react-app-purescript). Run `npm install` or `yarn install`.\r\n- Step by step instructions: \r\n - Create react application with TypeScript.\r\n \r\n ```shell\r\n npx create-react-app create-react-app-purescript --template typescript\r\n ```\r\n\r\n - Follow [the craco guide](https://github.com/gsoft-inc/craco/blob/master/packages/craco/README.md#installation) to overwrite parts of the `create-react-app` configuration.\r\n - Extend craco configuration to use [`craco-purescript-loader`](https://github.com/andys8/craco-purescript-loader).\r\n - Install PureScript (compiler) and initialize spago (package manager).\r\n \r\n ```shell\r\n npm install purescript spago --save-dev\r\n npx spago init\r\n ```\r\n\r\n - Add npm script in `package.json` and install dependencies with `npm install`\r\n \r\n ```json\r\n {\r\n \"postinstall\": \"spago build --deps-only\"\r\n }\r\n ```\r\n\r\n - Install [`react-basic`](https://github.com/lumihq/purescript-react-basic) and [`react-basic-hooks`](https://github.com/spicydonuts/purescript-react-basic-hooks).\r\n \r\n ```shell\r\n npx spago install react-basic react-basic-dom react-basic-hooks\r\n ```\r\n\r\n - Add a PureScript component: `Counter.ps`.\r\n \r\n ```haskell\r\n module Counter where\r\n\r\n import Prelude\r\n import Effect (Effect)\r\n import Effect.Unsafe (unsafePerformEffect)\r\n import React.Basic.DOM as R\r\n import React.Basic.Events (handler_)\r\n import React.Basic.Hooks\r\n import React.Basic.Hooks as React\r\n\r\n mkCounter :: ReactComponent {}\r\n mkCounter = unsafePerformEffect counter\r\n\r\n counter :: Effect (ReactComponent {})\r\n counter = do\r\n reactComponent \"Counter\" \\_ -\u003e React.do\r\n count /\\ setCount \u003c- useState 0\r\n let\r\n handleClick = handler_ \u003c\u003c\u003c setCount\r\n pure\r\n $ R.div_\r\n [ R.button { \r\n onClick: handleClick (_ - 1), \r\n children: [ R.text \"-\" ] }\r\n , R.div_ [ R.text (show count) ]\r\n , R.button { onClick: handleClick (_ + 1), \r\n children: [ R.text \"+\" ] }\r\n ]\r\n ```\r\n\r\n - Allow module import in TS: `purescript-module.d.ts`.\r\n - Import the component and use it: `App.tsx`.\r\n\r\n ```tsx\r\n import { mkCounter as Counter } from \"./Counter.purs\";\r\n // ...\r\n function App() {\r\n return \u003cCounter /\u003e;\r\n }\r\n ```\r\n- [`purescript-tsd-gen`](https://github.com/minoki/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`](https://github.com/gcanti/fp-ts).\r\n- Some examples:\r\n - [`purescript-react-realworld`](https://github.com/jonasbuntinx/purescript-react-realworld): A real world implementation of Conduit.\r\n - [`gatsby-purescript-example`](https://github.com/jonasbuntinx/gatsby-purescript-example): Simple example app using Gatsby.js with PureScript.\r\n - [`next-purescript-example`](https://github.com/jonasbuntinx/next-purescript-example): Simple example app using Next.js with PureScript.\r\n \r\n## 2. React Native with PureScript\r\n\r\n- `create-react-native-app purescript-app`\r\n- `pulp init --force`\r\n- `pulp build` \r\n- `src/Main.js`\r\n```javascript\r\nvar React = require(\"react\");\r\nvar RN = require(\"react-native\");\r\n\r\nexports.text = function(props){\r\n return function(str){\r\n return React.createElement(RN.Text, props, str); \r\n };\r\n};\r\n```\r\n- `src/Main.purs`\r\n\r\n```haskell\r\nmodule Main where\r\n\r\nforeign import data ReactElement :: Type\r\n\r\nforeign import text :: forall props. props -\u003e String -\u003e ReactElement\r\n\r\nmain :: ReactElement\r\nmain = text { \r\n style : { \r\n color : \"green\", \r\n fontSize : 50 \r\n } \r\n } \"Hello from PureScript!\" \r\n```\r\n\r\n- `./App.js`\r\n\r\n```javascript\r\nimport React from 'react';\r\nimport Main from \"./output/Main\";\r\n\r\nexport default class App extends React.Component {\r\n render() {\r\n return Main.main;\r\n }\r\n}\r\n```\r\n- `pulp build`\r\n- `yarn start` (npm may also work)\r\n","author":{"url":"https://github.com/lambda-forum","@type":"Person","name":"lambda-forum"},"datePublished":"2021-05-08T12:10:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/3/lambda-forum.github.io/issues/3"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:79b45c69-1301-7f7a-8ba6-af13654acf0c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 817A:985FF:B5A6B:E2E13:6991D6B3 |
| html-safe-nonce | d6dc153a7940cf866cedb47f829060c1d60987b4edff9d614aeb6b690c078eef |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTdBOjk4NUZGOkI1QTZCOkUyRTEzOjY5OTFENkIzIiwidmlzaXRvcl9pZCI6IjY0MTEzMDA3MTE5MDk0MTQ1NzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2fe355695895f1aac84847a761b0d78fbced76684782f2e292f77497c274bcab |
| hovercard-subject-tag | issue:880740209 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/lambda-forum/lambda-forum.github.io/3/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d17a91557fef20ee34018e7c549afc310948d009267d83fb9ac966736a72b813/lambda-forum/lambda-forum.github.io/issues/3 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d17a91557fef20ee34018e7c549afc310948d009267d83fb9ac966736a72b813/lambda-forum/lambda-forum.github.io/issues/3 |
| og:image:alt | 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-a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lambda-forum |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| turbo-cache-control | no-preview |
| go-import | github.com/lambda-forum/lambda-forum.github.io git https://github.com/lambda-forum/lambda-forum.github.io.git |
| octolytics-dimension-user_id | 83862771 |
| octolytics-dimension-user_login | lambda-forum |
| octolytics-dimension-repository_id | 365469812 |
| octolytics-dimension-repository_nwo | lambda-forum/lambda-forum.github.io |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 365469812 |
| octolytics-dimension-repository_network_root_nwo | lambda-forum/lambda-forum.github.io |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 848bc6032dcc93a9a7301dcc3f379a72ba13b96e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width