未経験からのReact入門『TypeScript』|レンダリング

概要

React は、Web ページに HTML をレンダリングすることを目標にしています。

大まかに以下のように ReactCreateElement で作成した HTML オブジェクトを、ReactDom.render()を使用して、DOM に HTML コードをレンダリングします。

ReactDOM.render(
  React.createElement('h1', {}, 'Hello World'),
  document.getElementById('root')
);

ハローワールドの出力

前回作成したプロジェクトの index.tsx を編集します。

// index.tsx

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const element = <h1>Hello, world!</h1>;
ReactDOM.render(
  <React.StrictMode>
    element
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

ノード

ReactDOM.render の第三引数にて DOM 要素を指定しているのがわかるかと思います。

document.getElementById('root')

これは出力先の DOM 要素を指定しており、参照する HTML は public.html になります。

つまり新しいノードでレンダリングを行いたい場合は、public.html に新しい出力先ノードを作成し、設定してあげるだけです。

例として以下の通りです。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC\_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC\_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC\_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC\_URL% in the tags above.
      It will be replaced with the URL of the \`public\` folder during the build.
      Only files inside the \`public\` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC\_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running \`npm run build\`.
    -->
    <title>React App</title>

  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <div id="element"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run \`npm start\` or \`yarn start\`.
      To create a production bundle, use \`npm run build\` or \`yarn build\`.
    -->

  </body>
</html>

新しく、以下ノードを追記してみました。

<div id="element"></div>

ノード作成が完了したら後は簡単です。

以下のようにレンダリングしましょう。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import reportWebVitals from './reportWebVitals';
const element = React.createElement('h1', {}, 'Hello World')
ReactDOM.render(
  element,
  document.getElementById('root')
);
ReactDOM.render(
  element,
  document.getElementById('element')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

まとめ

非常に簡単だったかと思います。

ノードの設定はいつでもできるように覚えておきましょう。