安裝React
npm install -g create-react-app
創建一個React項目
create-react-app demo
.....
npm start
Hello React
刪除src下的文件,重寫,hello world纔是第一步!
新建一個index.js
,引入react的包.
import React from 'react'
import ReactDom from 'react-dom'
再建個Hello.js
,這個裏面寫我們的hello world
,React理念組件化,我是這樣理解的.
import React, {Component} from 'react'
class Hello extends Component {
render() {
return(
<div>
Hello World!
</div>
)
}
}
export default Hello
這裏的{Componet}
是ES6的解構寫法.
將這個Hello.js
文件在index.js
中引入進去.
import React from 'react'
import ReactDom from 'react-dom'
import Hello from './Hello'
ReactDom.render(<Hello />, document.getElementById('root'))
這裏的root
節點在public
文件夾下的index.html
裏面.
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></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>
然後我們npm start
運行下:
努力,gogogo