Reactjs 如何从由<;渲染的组件更改管线;路线>;你有什么反应?

Reactjs 如何从由<;渲染的组件更改管线;路线>;你有什么反应?,reactjs,react-router,Reactjs,React Router,我正在渲染路由中的主组件,以便将状态作为道具传递给该组件 class App extends React.Component { constructor(props){ super(props) this.state = { page: 'false' }; } render() { return ( <Router>

我正在渲染
路由中的
组件,以便将状态作为道具传递给该组件

class App extends React.Component {
    constructor(props){
        super(props)
        this.state = {
            page: 'false'
        };
    }
    render() {
        return (
            <Router>
                <div>
                    <Switch>
                        <Route exact path='/' render={()=><Home page={this.state.page}/>} />
                        <Route exact path='/projects' component={Projects} />
                        <Route render={function(){
                            return <p>Not Found</p>
                        }} />
                    </Switch>
                </div>
            </Router>
        )
    }
}
如何在保留组件道具的同时更改组件的路线

更新 我已经使用createBrowserHistory创建了一个History.js

import { createBrowserHistory } from 'history'

export default createBrowserHistory()
并更新了App.js

import history from '../history';

class App extends React.Component {
    constructor(props){
        super(props)
        this.state = {
            page: 'false'
        };
    }
    render() {
        return (
            <Router>
                <div>
                    <Switch>
                        <Route exact path='/' render={()=><Home history={history} page={this.state.page}/>} />
                        <Route exact path='/projects' component={Projects} />
                        <Route render={function(){
                            return <p>Not Found</p>
                        }} />
                    </Switch>
                </div>
            </Router>
        )
    }
}
从“../history”导入历史记录;
类应用程序扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
页面:“错误”
};
}
render(){
返回(
} />
)
}
}

因此,现在当我单击
Home
中的按钮时,url会转到/projects,但视图仍然呈现
Home
,而不是
projects
。在history.push发生后,我如何渲染
项目

我想这就是你需要的答案我不确定如何正确构造我的代码以与router一起使用你只需要与你想要使用history prop的组件一起使用,比如
导出默认witRouter(Home)我得到了一些错误:index_bundle.js:1834未捕获错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:object。不变冲突:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。检查
Route
的渲染方法。有哪些错误
import history from '../history';

class App extends React.Component {
    constructor(props){
        super(props)
        this.state = {
            page: 'false'
        };
    }
    render() {
        return (
            <Router>
                <div>
                    <Switch>
                        <Route exact path='/' render={()=><Home history={history} page={this.state.page}/>} />
                        <Route exact path='/projects' component={Projects} />
                        <Route render={function(){
                            return <p>Not Found</p>
                        }} />
                    </Switch>
                </div>
            </Router>
        )
    }
}