gpt4 book ai didi

javascript - 通过 ReactDOM 渲染多个组件

转载 作者:行者123 更新时间:2023-11-30 11:43:47 25 4
gpt4 key购买 nike

目前无法弄清楚为什么我的代码没有呈现。会喜欢你们中一些杰出的 react.js 编码员的一些建议。真的不明白哪里出了问题 - 我觉得应该可以。

这是我的 App.js

import React from 'react';
import ReactDOM from 'react-dom'


import HeaderNavigation from './HeaderNavigation';
import Body from './Body';
import Footer from './Footer';




ReactDOM.render(
<HeaderNavigation/>,
<Body/>,
<Footer/>,
document.getElementById('app'));

这是组件。

body .js

import React from "react";

/* Styles */
import Button from 'react-bootstrap/lib/Button';
import Grid from 'react-bootstrap/lib/Grid';
import Jumbotron from 'react-bootstrap/lib/Jumbotron';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';

export default class Body extends React.Component {
render() {
return (
<div>
<Jumbotron>
<Grid>
<h1>Mr Mac</h1>
<p>Poodle Box</p>
</Grid>
</Jumbotron>

<Grid>
<Row>
<Col md={4}>
<h2>Luxurious Dog Clothes</h2>
<p>So luxury much now</p>
<p><Button>View details »</Button></p>
</Col>
<Col md={4}>
<h2>Delectable Dog Délectants</h2>
<p>Food</p>
<p><Button>View details »</Button></p>
</Col>
<Col md={4}>
<h2>Toys</h2>
<p>MMmhmmmm</p>
<p><Button>View details</Button></p>
</Col>
</Row>
</Grid>
</div>
);
}
}

页脚.js

import React from 'react';
import Grid from 'react-bootstrap/lib/Grid';

export default class Footer extends React.Component {
render() {
return (
<Grid>
<hr />
<footer>
<p>© Company 2014</p>
</footer>
</Grid>
);
}
}

HeaderNavigation.js

import React from 'react';
import Navbar from 'react-bootstrap/lib/Navbar';
import Nav from 'react-bootstrap/lib/Nav';

export default class HeaderNavigation extends React.Component {

render() {
let brand = <a href='#'>Project Name</a>;
return (
<Navbar brand={brand} fixedTop inverse toggleNavKey={0}>
<Nav right eventKey={0}>
<InlineLogin className='navbar-form' />
</Nav>
</Navbar>
);
}
}

索引.html

<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
<title>DOGBOXUNITED</title>
<span id = 'Heading'> </span>
</head>

<body>
<div id='app'> </div>
<script src="output/myCode.js"></script>
</body>

</html>

网络包配置

var webpack = require("webpack");
var path = require("path");

var DEV = path.resolve(__dirname, "dev");
var OUTPUT = path.resolve(__dirname, "output");

var config = {
entry: DEV + "/App.js",
output: {
path: OUTPUT,
filename: "myCode.js"
},
module: {
loaders: [{
include: DEV,
loader: "babel",
}]
}
};

module.exports = config;

非常感谢任何帮助!

最佳答案

一切看起来都正常,除了这个:

ReactDOM.render(
<HeaderNavigation/>,
<Body/>,
<Footer/>,
document.getElementById('app')
);

因为你想呈现多个元素,将它们包装在一个 div 中它会起作用。试试这个:

ReactDOM.render(
<div>
<HeaderNavigation/>
<Body/>
<Footer/>
</div>,
document.getElementById('app')
);

检查 jsfiddle:https://jsfiddle.net/4m3m7653/

关于javascript - 通过 ReactDOM 渲染多个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690545/

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