私は、1ページではないreactjsを使ってWebアプリケーションを作成したいのですが。 すべての反応コードを1つのファイルにコンパイルし、アプリケーションのすべてのページにロードする必要がありますか?次に、公開する関数を使用して必要なコンポーネントをレンダリングしますか?私が増加し、品質と難易度の2つの方法が、参照してくださいねindex.jsルータコンポーネントなしでreactを使用する
import React from 'react';
import ReactDOM from 'react-dom';
import Clock from './Clock';
import HelloWorld from './HelloWorld';
import OtherComponent from './OtherComponent';
const APPS = {
Clock,
HelloWorld,
OtherComponent
};
const MyReactRender = react => {
let component = react.getAttribute('data-react');
let App = APPS[component];
if(App != undefined) {
ReactDOM.render(<App />, document.getElementById(component));
}
}
document.querySelectorAll('[data-react]').forEach(MyReactRender);
私はちょうど 'if(document.getElementById( 'Clock')){/ * activate react component * /}' –
を入力していただきありがとうございます。 しかし、アプリケーションが大きく成長する可能性があるので、この方法を使用するのは正しいでしょうか? – MrGilbertMan
いいえ、間違いなく推奨される方法ですが、唯一の本当の欠点はバンドルのサイズとますます多くのifsです。より良い方法は、webpackを使用して、アプリケーションの異なる部分をバンドルし、それぞれの異なるページにロードすることだけです。 –