前端學習-React基礎

安裝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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章