【问题标题】:Component does not render组件不渲染
【发布时间】:2020-08-18 10:18:44
【问题描述】:

如果我们点击帖子链接,blog.js 的执行将从这里开始。

我在 post.js 中调用了 history.push 两次,但是 new-post 没有呈现,就像在 newpost.js 中指定的控制台日志一样。 ComponentDidMount 和 render 方法没有执行,但是它正在执行posts.js中的控制台日志。

博客.js:

class Blog extends Component {
    render () {
        return (
            <div  className="Blog">
               <ul>
                   <li>
                       <Link to="/posts" exact>Posts</Link>
                   </li>
                   <li>
                       <Link to="/new-post">New Post</Link>
                   </li>
                            
               </ul>
               <Switch>
                   <Route path="/new-post" render={()=>(<Newpost/>)}/>
                   <Route path="/posts" component={Posts}/>
                   <Route path="/" exact component={Posts}/>
                   <Route render={()=><h1>Not Found</h1>}/>
               </Switch>
            </div>
        );
    }
}

Posts.js(当我们在 blog.js 中调用帖子链接时呈现):

class Posts extends Component {
    componentDidMount(){
            console.log("render");
        });
    }

    postselect = (id) => {
            this.props.history.push('/new-post');
            console.log("hello");
            console.log("hello");
            console.log("hello");
            console.log("hello");
            
            this.props.history.push('/posts');
    }
    render()
    {
        console.log("posts render");
        return(
            <div>
            <button onClick={this.postselect}>submit</button>
            </div>);
    }
}

newposts.js(如果我们调用这个路由应该渲染):

class NewPost extends Component {

    componentDidMount(){
        console.log("hellonewpost");
    }
    
    render () {
        console.log("newpost render");
        return (
            <div className="NewPost">
                ...
            </div>
        );
    }
}

【问题讨论】:

  • 试试这个。
  • 同样的事情发生了......没有变化
  • 可能是因为你在 switch 之外使用了 Link 元素?
  • 用Router组件包裹
    再试一次

标签: reactjs react-redux react-router react-hooks react-state


【解决方案1】:

您不能在同一个函数中两次发布推送历史记录。我不太确定你想做什么,但我假设你想做这样的事情:

 postselect =(id)=>
    {
            this.props.history.push('/new-post');
    }

在您的 New-Post 组件中:

componentDidMount{
 console.log("hello");
 console.log("hello");
 console.log("hello");
 console.log("hello");
 this.props.history.push('/posts');

【讨论】:

    【解决方案2】:

    您在一个函数中同时执行 2 个history.pushes。

    请注意 - 一旦您的点击处理函数完成执行,javascript 引擎将执行 history.push。到函数执行完成时,历史对象已被修改(第二次)以具有 /posts,因此永远不会呈现 /new-post

    一般来说,最好将history.push 作为函数中的最后一行代码。

    编辑(根据评论)

    这里是a demo。转到Home.js 并单击按钮并观察console.logs。

    【讨论】:

    • 如果我在 posts.js 中保留一个 push('/new-post') 然后一些控制台日志...而不是 2 push .... 那么它会导航到 new- post' 然后控制台日志正在输出......但是你说JavaScript引擎会在执行整个函数后执行history.push......???
    • yes... 在您的函数中,如果您先执行 history.push,然后执行 console.log,则首先打印 console.log,然后(在完成函数执行后),路由完成.. . 我已经用演示更新了答案...所以检查 console.logs 那里。
    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 2019-06-20
    • 2016-02-09
    • 2020-03-01
    • 2018-01-24
    • 2018-12-25
    • 2021-03-19
    • 2019-09-23
    相关资源
    最近更新 更多