簡體   English   中英

基本解釋了npm開始與我的reactjs代碼有關以及如何從瀏覽器運行它

[英]basic explain for what does npm start do with my reactjs code and how to run it from browser

我對這個基本問題感到抱歉-但我試圖理解它。

我開始學習reactjs( https://reactjs.org/tutorial/tutorial.html ),並創建了reactjs的新項目(使用npm .. create-react-app ...)。

我刪除src中的所有文件(即本教程中的內容)並創建兩個文件

  1. index.js
  2. index.scc

在文件index.js中,我添加代碼:

import React from 'react';
import './index.css';

class ShoppingList extends React.Component{
    render(){
        return(
            <div className="shopping-list">
                <h1>Shopping List for{this.props.name}</h1>
                    <ul>
                        <li>App1</li>
                        <li>App2</li>
                        <li>App3</li>
                    </ul>
            </div>
        );
    }

現在,當我從文件夾“ src”運行“ npm start”時,我什么也看不到。 我也無法理解如何僅從瀏覽器運行它

您需要在dom中渲染ShoppingList組件,以查看其結果。 利用ReactDOM.render()方法做到這一點

class ShoppingList extends React.Component{
    render(){
        return(
            <div className="shopping-list">
                <h1>Shopping List for{this.props.name}</h1>
                    <ul>
                        <li>App1</li>
                        <li>App2</li>
                        <li>App3</li>
                    </ul>
            </div>
        );
    }

ReactDOM.render(<ShoppingList name={"Shop"}/>, document.getElementById('root')); // here root is the DOM element in `index.html`

的index.html

<body>
    <div id="root"/>
    <script src="path/to/bundle.js"></script>  // this is the bundled file generated using webpack
</body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM