官术网_书友最值得收藏!

Splitting up our app

To organize our app a bit better (and to do some magic in the next section), let’s move our JSX into a separate file from our ReactDOM.render. This will ensure good separation of concerns throughout our file structure.

Next to index.js, in our src folder, create a file called App.js. Inside, we’ll just make a function called App, which returns our JSX:

import React from 'react';

const App = () => {
return <h1>Hello from React!!</h1>
};

export default App;

Note the export statement at the bottom; this means when we import our file, we’ll automatically get this function as the default import. We'll see an example of non-default imports down the line, which will make this clearer.

If we jump back to index.js, we can now import App from './App’. Then, we render it, as shown:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App'

ReactDOM.render(<App />, document.getElementById('root'));

Note that we are using it just like an HTML (or rather, JSX) tag. We’ll talk more about why in the coming chapters; for now, the important thing is that our app is a bit more organized, with our view logic (the JSX) separate from the render logic (ReactDOM.render).

主站蜘蛛池模板: 仙居县| 罗田县| 陆良县| 西平县| 泸水县| 汽车| 诸暨市| 中宁县| 二连浩特市| 怀化市| 绥江县| 泊头市| 南通市| 塔河县| 汤阴县| 遂溪县| 洪雅县| 册亨县| 龙陵县| 安岳县| 轮台县| 台州市| 曲阳县| 灵寿县| 忻州市| 青田县| 敖汉旗| 拉萨市| 南乐县| 清徐县| 思茅市| 星子县| 齐齐哈尔市| 灯塔市| 镇原县| 紫金县| 巴林右旗| 汉阴县| 定南县| 巢湖市| 龙山县|