简体   繁体   English

反应路由器渲染一次组件之后只有 url 改变

[英]React router renders component once after that only url changes

I am having a navbar and a side bar for my page.我的页面有一个导航栏和一个侧栏。

Navbar consists of home and blogs导航栏由主页和博客组成

Blogs will render BlogHome Component which will fetch links from db and on click of any link will render BlogContent component.博客将呈现 BlogHome 组件,该组件将从 db 获取链接,单击任何链接将呈现 BlogContent 组件。

Lets say the side bar has Blog1,Blog2 and Blog3 listed.假设侧边栏列出了 Blog1、Blog2 和 Blog3。 If I click Blog1 it renders Blog1's content properly to its side, but if I click Blog2 again it just changes URL but not the Blog2's content.如果我单击 Blog1,它会将 Blog1 的内容正确地呈现到其一侧,但如果我再次单击 Blog2,它只会更改 URL 而不是 Blog2 的内容。

Please take a look at my code:请看一下我的代码:

Navbar.js导航栏.js

      <Router>
            <Container className="p-0" fluid={true}>
                <Navbar className="border-bottom" bg="transparent" expand="lg">
                    <Navbar.Brand>{global.config.appname}</Navbar.Brand>
                    <Navbar.Toggle className="border-0" aria-controls="navbar-toggle" />
                    <Navbar.Collapse id="navbar-toggle">
                        <Nav className="ml-auto">
                            <Link className="nav-link" to="/">Home</Link>
                            <Link className="nav-link" to="/blogs/main">Blogs</Link>
                            <Link className="nav-link" to="/contact">Contact</Link>
                        </Nav>
                    </Navbar.Collapse>
                </Navbar>
                </Container>
                <Switch>
                    <Route exact path="/" component={Home}></Route>
                    <Route exact path="/blogs/main" component={BlogHome}></Route>
                </Switch>
            </Router>

BlogHome.js BlogHome.js


export default class BlogHome extends Component {
    constructor(props)
    {
        super(props);
        this.state = { data: null,route:null };
    }
    componentDidMount = () => {
        console.log("BlogHome");
        BlogDataService.getAll().then(data => {
            let data_temp = []
            let cnt = 0;
            for (let item of data.data) {

                data_temp.push(
                <MenuItem key={cnt++} icon={<FaBlog />}>
                    <Link to={"/blogs/main/" + item.id}>{item.title}</Link>
                </MenuItem>
                );
                
            }
            this.setState({ data: data_temp });
        })
    }
    render() {
        return (
            <Router>
                <div style={{ display: "flex" }}>
                <ProSidebar>
                    <Menu iconShape="square">
                        {this.state.data}
                    </Menu>
                </ProSidebar>
                    
                <Switch>
                    <Route exact path={"/blogs/main/:blogId"} component={BlogContent}></Route>
                </Switch>
                </div>
            </Router>
        );
    }
}

BlogContent.js博客内容.js


export default class BlogContent extends Component {
    constructor(props) {
        super(props);
        const contentState = convertFromRaw(content);
        this.state = {
            contentState,
            item_id: this.props.match.params.blogId,
            title:null
        }
        console.log(this.props.match);
    }

    onContentStateChange: function = (contentState) => {
        this.setState({
            contentState,
        });
    };
    componentDidMount = () => {
        BlogDataService.get(this.state.item_id).then(data => {
            console.log(data);
            this.setState({ title: data.data.title })
        });
    }
    render() {
        const { contentState } = this.state;
        return (
            <Router>
            <div style={{padding:"10px"}}>
                <div style={{padding:"50px",fontSize:"50px"}}>
                    {this.state.title}
                </div>
            <Editor
                wrapperClassName="demo-wrapper"
                editorClassName="demo-editor"
                onContentStateChange={this.onContentStateChange}
                    />
            <Route exact path={"/blogs/main/1"} component={BlogContent}></Route>
                </div>
            </Router>
        );
    }
}

Thank you for reading:)感谢您阅读:)

your item_id is set only one time and it is not changing at all.您的 item_id 仅设置一次,并且根本没有改变。 On first time when component load it will work but when you are doing second time you are passing new item id but component is not aware about this change hence not able to do anything.第一次加载组件时它会起作用,但是当您第二次执行时,您正在传递新的项目 ID,但组件不知道此更改,因此无法执行任何操作。

Try to create a function which fetch data.尝试创建一个获取数据的 function。 Same function call it in componentDidmount.同样的 function 在 componentDidmount 中调用它。 Now when it is getting new props it is time to check.现在,当它获得新道具时,是时候检查一下了。 Use componentDidUpdate.使用 componentDidUpdate。

componentDidUpdate(prevProps, prevState){ 
   if(prevProps.blogId != this.props.blogId){
        this.setState({
           item_id: this.props.blogId 
       }, () => { call the function to get the data } )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM