gpt4 book ai didi

reactjs - React js 做通用 header

转载 作者:行者123 更新时间:2023-12-03 13:17:10 33 4
gpt4 key购买 nike

我是 ReactJS 新手。我需要有一个通用标题并根据路线变化更改标题。我需要创建一个 header.jsx 文件并导入它吗?否则,如何使用路由渲染 header (公共(public)文件)?

我的路由部分如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
import Home from './Home.jsx';
import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router';

ReactDOM.render((
<Router history = {browserHistory}>
<Route path = "/home" component = {Home} />
<Route path = "/" component = {App}>
</Route>
</Router>
));

最佳答案

这应该有效:

header.jsx:

class Header extends Component {
render() {
return (<div>Your header</div>);
}
}

第一页.jsx:

class FirstPage extends Component {
render() {
return (<div>First page body</div>);
}
}

第二页.jsx

class SecondPage extends Component {
render() {
return (<div>Second page body</div>);
}
}

app.jsx:

import Header from './header.jsx';

class App extends Component {
render() {
return (
<div>
<Header />
{this.props.children}
</div>
);
}
}

web-app.jsx:

import App from './app.jsx';
import FirstPage from './first-page.jsx';
import SecondPage from './second-page.jsx';

ReactDOM.render(
<Router history = {browserHistory}>
<Route path = "/" component = {App}>
<Route path = "/first" component = {FirstPage}>
<Route path = "/second" component = {SecondPage}>
</Route>
</Router>
);

关于reactjs - React js 做通用 header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510111/

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