gpt4 book ai didi

javascript - react 组件没有出现

转载 作者:行者123 更新时间:2023-11-30 11:36:52 24 4
gpt4 key购买 nike

我在 https://bitbucket.org/codyc54321/anthony-project-react 有一个仓库

我的主页显示链接,当您单击链接时,react-router 会工作。但是组件的文本没有显示,比如 <div>Login page</div> , <div>Homepage</div>

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, IndexRoute, browserHistory } from 'react-router';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';

import reducers from './reducers';

import Nav from './Nav';
import Home from './components/Home';
import Login from './components/Login';
import SignupMain from './components/SignupMain';


const createStoreWithMiddleware = applyMiddleware()(createStore);

ReactDOM.render(
(
<Provider store={createStoreWithMiddleware(reducers)}>
<Router history={browserHistory}>
<Route path="/" component={Nav}>
<IndexRoute component={Home}/>
<Route path='/login' component={Login}/>
<Route path='/signup-main' component={SignupMain}/>
</Route>
</Router>
</Provider>
), document.querySelector('.container'));

导航.js:

import React from 'react';
import { Link } from 'react-router';

export default class Nav extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<div>
<nav className="navbar navbar-default">
<div className="container-fluid">
<div className="navbar-header navbar-brand">
<a href="/">Home</a>
</div>
</div>
<div id="navbar" className="navbar-right">
<ul className="nav navbar-nav">
<li><Link activeClassName="active" to="/" onlyActiveOnIndex>Home</Link></li>
<li><Link activeClassName="active" to="/login" onlyActiveOnIndex>Login</Link></li>
<li><Link activeClassName="active" to="/signup-main" onlyActiveOnIndex>Signup</Link></li>
</ul>
</div>
</nav>
</div>
)
}
}


// export Nav as default;

组件/Home.js:

import React, { Component } from 'react';

export default class Home extends Component {
render() {
return (
<div>Homepage</div>
);
}
}

为什么这些组件没有显示在页面上?

最佳答案

由于您的其他路由嵌套在“Nav”组件的路由内,因此它应该在某处呈现其子项。为此,您必须将 this.props.children 放在您想在 Nav 组件的 JSX 中呈现嵌套组件的位置。

export default class Nav extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<div>
<nav className="navbar navbar-default">
<div className="container-fluid">
<div className="navbar-header navbar-brand">
<a href="/">Home</a>
</div>
</div>
<div id="navbar" className="navbar-right">
<ul className="nav navbar-nav">
<li><Link activeClassName="active" to="/" onlyActiveOnIndex>Home</Link></li>
<li><Link activeClassName="active" to="/login" onlyActiveOnIndex>Login</Link></li>
<li><Link activeClassName="active" to="/signup-main" onlyActiveOnIndex>Signup</Link></li>
</ul>
</div>
</nav>
<div>{this.props.children}</div>
</div>
)
}
}

关于javascript - react 组件没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092317/

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