gpt4 book ai didi

javascript - ReactJS 渲染带有 url 变化的 subview

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

我正在尝试根据组件链接上的导航菜单将 subview (组件)渲染到,相应地更改 url。我的路由器是:

const router = new Router(on => {

on('*', async (state, next) => {
const component = await next();
return component && <App context={state.context}>{component}</App>;
});

on('/profile', async () => <ProfilePage />);

on('/contact', async () => <ContactPage />);

on('/login', async () => <LoginPage />);

on('/register', async () => <RegisterPage />);

on('*', async (state) => {
const content = await http.get(`/api/content?path=${state.path}`);
return content && <ContentPage {...content} />;
});

on('error', (state, error) => state.statusCode === 404 ?
<App context={state.context} error={error}><NotFoundPage /></App> :
<App context={state.context} error={error}><ErrorPage /></App>);

});

class ProfilePage extends React.Component {

render() {

var user = "Username";

return (
<div>
<ProfileHead>
<Cover user={user} />
<Menu />
<SubMenu />
</ProfileHead>
<ProfileBody>
<ProfileHome />
</ProfileBody>
</div>
);
}

}

class Menu extends React.Component {

render() {

return (
<div className="menu">
<div className="menu-inner">
<Nav followers="522234" following="133234" />
</div>
</div>
);
}

}

class Nav extends React.Component {

render() {
return (
<div>
<a href="#" className="menu-link active">Profile</a>
<a href="#" className="menu-link">Portfolio</a>
<a href="#" className="menu-link">Activity</a>
<a href="#" className="menu-link">Following <span className="followCount">{this.props.following}</span></a>
<a href="#" className="menu-link">Followers <span className="followCount">{this.props.followers}</span></a>
</div>
);
}
}

我不确定这是如何实现的。任何帮助将不胜感激。我是 React 的新手,正在慢慢学习。

最佳答案

我建议使用 react-router

您可以创建嵌套路由,每个路由都加载其他子组件

var Router = require('react-router');
var Route = Router.Route;

// declare our routes and their hierarchy
var routes = (
<Route handler={App}>
<Route path="about" handler={About}/>
<Route path="inbox" handler={Inbox}/>
</Route>
);

关于javascript - ReactJS 渲染带有 url 变化的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32283414/

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