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

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

主站蜘蛛池模板: 牟定县| 满洲里市| 吴旗县| 武乡县| 甘谷县| 永修县| 罗定市| 嘉鱼县| 灯塔市| 晋城| 巫溪县| 柘荣县| 彰化市| 深圳市| 山东省| 西丰县| 南漳县| 吴江市| 从江县| 丁青县| 峡江县| 奉贤区| 天镇县| 彭州市| 安宁市| 西安市| 黎平县| 定西市| 乳山市| 同仁县| 遂平县| 尚志市| 萍乡市| 松原市| 京山县| 达拉特旗| 凤翔县| 温宿县| 淮南市| 河间市| 平湖市|