gpt4 book ai didi

reactjs - React 组件未呈现为 index.html

转载 作者:行者123 更新时间:2023-12-05 06:41:34 24 4
gpt4 key购买 nike

我真的是 ReactJS 的初学者。组件未呈现,我浪费了很多时间进行搜索但未成功。我正在关注 tutorial来自这个系列。我已经从这个 link 配置了 webpack-dev-server .根据教程检查下面的文件层次结构。

enter image description here

请让我知道问题。

index.html

<!DOCTYPE html>
<html>
<head>
<title>React</title>
</head>

<body>
<div id="app"></div>
<script src="app.js"></script>
</body>
</html>

App.js

import React from 'react';  
class AppFirst extends React.Component{
render(){
return(
<div>
<h1>Hello ReactJs</h1>
</div>
);
}
}
var appElement = <AppFirst />;
Raect.render(appElement, document.getElementById("app"));

包.json

    {
"name": "react-for-everyone",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "./node_modules/.bin/webpack-dev-server --content-base"
},
"author": "",
"license": "ISC",
"devDependencies":{
"babel-core": "^6.1.*",
"babel-loader": "^6.2.*",
"babel-preset-es2015": "^6.16.*",
"babel-preset-react": "^6.16.*",
"webpack": "^1.13.*",
"webpack-dev-server": "^1.16.*"
},
"dependencies":{
"react": "^15.3.*",
"react-dom": "^15.3.*"
}
}

webpack-config.js

    module.exports ={
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/App.js'
],
output: {
path: path.resolve(__dirname, "react-for-everyone"),
filename: 'app.js',
publicPath: 'http://localhost:8080'
},
devServer: {
contentBase: "./react-for-everyone",
},
module:{

loaders:[{
test: "/\.jsx?$/",
exclude: /node_modules/,
loader: "babel",
query: {
preset: ['es2015', 'react']
}
}]
}
};

最佳答案

React.render() 不再与当前版本一起使用。 React 已从 0.14 版本拆分库。除了 React.render()

中的拼写错误外,您还需要使用 ReactDOM.render()
import React from 'react'; 
import ReactDOM from 'react-dom';

class AppFirst extends React.Component{
render(){
return(
<div>
<h1>Hello ReactJs</h1>
</div>
);
}
}
var appElement = <AppFirst />;
ReactDOM.render(appElement, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="app"></div>

在您的 webpack 中,您在 test: "/\.jsx?$/" 中放置了引号,您不应该使用它们。 测试:/\.jsx?$/。它应该是一个正则表达式而不是一个字符串

您需要做的其他更正。

  1. 将您的 webpack-config.js 重命名为 webpack.config.js

  2. 在 webpack 加载器中,您需要定义 presets 而不是 preset

  3. 使用 npm install -S path 和 webapck 作为 var path = require('path');

    在 package.json 中包含路径
  4. 使用包含在 src 中的 index.html 更改您的目录结构

  5. 你最终的 webpack 看起来像这样

webpack.config.js

 var path = require('path');

module.exports ={
context: path.join(__dirname, "src"),
entry: [
'./App.js'
],
output: {
path: __dirname + "/src",
filename: 'bundle.js',
},
module:{

loaders:[{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
}]
}
};
  1. package.json 中的脚本更改为 "dev": "./node_modules/.bin/webpack-dev-server --content-base src"

  2. 最后在执行 npm run dev 后打开 http://localhost:8080/index.html

关于reactjs - React 组件未呈现为 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40043522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com