Created
November 24, 2020 14:31
-
-
Save hagevvashi/527a525e22c07ec955c17043e71550d6 to your computer and use it in GitHub Desktop.
reactとstyled-componentsを用いた実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { FC } from "react"; | |
import { renderToString } from "react-dom/server"; | |
import { ServerStyleSheet, createGlobalStyle, css } from "styled-components"; | |
import { FormComponent } from "./form"; // RoRでレンダリングされるform要素をトレースしたもの | |
import { dummyData } from "./data"; // RoRから引き継ぎたいjsonデータをオブジェクトで | |
// reset css 適当に | |
const reset = css` | |
input, | |
textarea { | |
padding: 0; | |
} | |
`; | |
const GlobalStyle = createGlobalStyle` | |
${reset} | |
`; | |
const sheet = new ServerStyleSheet(); | |
const Main: FC = () => { | |
return ( | |
<> | |
<GlobalStyle /> | |
<div id="app1" /> | |
<div | |
id="app2" | |
data-json-data={JSON.stringify(dummyData)} | |
/> | |
<FormComponent /> | |
</> | |
); | |
}; | |
const reactDom = renderToString(sheet.collectStyles(<Main />)); | |
const styleTags = sheet.getStyleTags(); | |
res.write( | |
templateHtml( | |
"App", | |
reactDom, | |
styleTags, | |
// jsファイルの読み込み(optimization特にしていない場合) | |
`<script src="/bundle.js"></script>` | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment