在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):joelburget/react-haskell开源软件地址(OpenSource Url):https://github.com/joelburget/react-haskell开源编程语言(OpenSource Language):Haskell 86.7%开源软件介绍(OpenSource Introduction):React-HaskellAs crazy as it seems, using React and Haskell together just may be a good idea. I was driven to create this thing because I had a large existing Haskell codebase I wanted to put online. However, even without existing code, I think a lot of problems are better modeled in Haskell than JavaScript or other languages. Or you might want to use some existing Haskell libraries. ExamplesLet's put a simple paragraph on the page: sample :: ReactNode a
sample = p_ [ class_ "style" ] $ em_ "Andy Warhol"
main :: IO ()
main = do
Just doc <- currentDocument
let elemId :: JSString
elemId = "inject"
Just elem <- documentGetElementById doc elemId
render sample elem That creates a DOM node on the page that looks like: <p class="style">
<em>Andy Warhol</em>
</p> We can make that a little more complicated with some more child nodes. sample :: ReactNode a
sample = div_ [ class_ "beautify" ] $ do
"The Velvet Underground"
input_
"Lou Reed" But of course that input doesn't do anything. Let's change that. sample :: JSString -> ReactNode JSString
sample = div_ $ do
"Favorite artist:"
input_ [ onChange (Just . value . target) ]
text str Getting StartedThe first step is a working GHCJS installation. The easiest way is to download a virtual machine with GHCJS pre-installed. I recommend ghcjs-box. Now that GHCJS is installed we can use cabal to create a project. $ mkdir project
$ cd project
$ cabal init # generate a .cabal file Now edit the cabal file to include dependencies. build-depends:
base >= 4.8 && < 5,
ghcjs-base,
ghcjs-dom,
react-haskell >= 1.3 Now we can write sample :: ReactNode a
sample = p_ [ class_ "style" ] $ em_ "Andy Warhol"
main :: IO ()
main = do
Just elem <- elemById "id"
render sample elem Next StepsReferenceAdditional ResourcesIs it Right for Me?React-Haskell is a great tool for building web UI from Haskell. However, you may want to consider the alternatives:
Small Print |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论