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

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).

主站蜘蛛池模板: 洪湖市| 定远县| 万州区| 遵化市| 奈曼旗| 威信县| 内乡县| 隆尧县| 延寿县| 内丘县| 逊克县| 贵德县| 怀集县| 盘山县| 突泉县| 凤庆县| 夏邑县| 军事| 长武县| 保亭| 福安市| 乌拉特后旗| 崇左市| 东城区| 鄂尔多斯市| 洪江市| 富宁县| 尖扎县| 佛山市| 龙岩市| 元朗区| 南皮县| 洛浦县| 来凤县| 潞城市| 乐都县| 遂川县| 平定县| 日照市| 长子县| 广饶县|