简体   繁体   English

React 组件渲染延迟 1 圈

[英]React component renders 1 turn latter

I'm new to React, I didnt know how to call the problem I'm facing我是 React 新手,我不知道如何称呼我面临的问题

I have a list of friends, and when the list is empty a message is displayed indicating "Add friends to be displayed here"我有一个朋友列表,当列表为空时,会显示一条消息,指示“添加要在此处显示的朋友”

and when I add a friend the message should dissapear当我添加朋友时,消息应该消失

the thing is that I need to do it with a life cycle method, like componentDidUpdate() for example.问题是我需要使用生命周期方法来完成它,例如 componentDidUpdate()。

The functionality is there, but as in the title, 1 turn or movement as I call it later功能是存在的,但正如标题所示,我稍后称之为 1 转或移动

I will explain:我会解释:

I see the message to add friends, but when I click to add a friend it doesnt dissapear until I add a second friend.我看到添加朋友的消息,但是当我单击添加朋友时,它不会消失,直到我添加第二个朋友。 And the message doesnt reapears when I delete all the friends, but when I add new friend, so its a turn latter, I do a thing and in the next movement the component is rerendered当我删除所有朋友时,该消息不会再次出现,但是当我添加新朋友时,所以轮到后者,我做一件事,然后在下一个动作中重新渲染组件

I'm sure its something simple, but I have been struggling with it for two days and I cant find how to make it work instantly.我确定它很简单,但是我已经为此苦苦挣扎了两天,但我找不到如何让它立即工作。

here is the code这是代码

import React, { Component } from 'react';
import Title from './title';

/* I'm using this var, because when I try to set it with a state I get an infinite loop */
var title = "Add friends to view them here"; 

class FriendList extends Component {
    constructor(props){
        super(props)

    }   

    componentWillReceiveProps(nextProps){
/* friendList is an array with friends objects */
        if(this.props.friendList.length > 0){
            console.log("this is "+ title);
            title = "";
        }else{
            title = "Add friends to view them here";
            console.log("this is "+ title);

        }
    }

 render(){

  let friends = this.props.friendList;
   return(
    <div className="friend-list">
           <p>{title}</p>
     <div> 
        {Object.keys(friends).map((key) => {
        var friend = friends[key];
         return (
         <div className="friendData">         
          <img className="friendPhoto"src={friend.profilePic} alt={friend.profilePic}/>
            <div className="friend">
                {friend.name}
                <br/> 
                {friend.city}
            </div>                
           <button className="delete" onClick={() => this.props.deleteFriend(friend)} >X</button>     
         </div> 
          ); 
        } 
     )}

    </div>
  </div>)  
 }
}




export default FriendList;

Please include a screenshot of the Browser React Debugger extension with all the variables,state,props expanded.请包含 Browser React Debugger 扩展的屏幕截图,其中包含所有变量 state,props 扩展。

It might be that your component is not re-rendering.可能是您的组件没有重新渲染。 I had this kind of issue before.我以前也遇到过这种问题。

Also, can you try closing the developer tools (F12).另外,您能否尝试关闭开发人员工具(F12)。 I had an issue before where my app does 1 tick only if my Dev Tool is open.只有在我的开发工具打开的情况下,我的应用程序才会执行 1 次滴答之前,我遇到了一个问题。 Where my element is supposed to change color only when I hover-in, and back to original color when I hover-out, but when my dev tools is open, it's stuck to the changed color even if I already hover-out.我的元素应该只在我悬停时改变颜色,当我悬停时恢复到原始颜色,但是当我的开发工具打开时,即使我已经悬停,它也会停留在改变的颜色上。

You can conditionally render the title您可以有条件地呈现标题

 <p>{this.props.friendList.length === 0 ? title : null}</p>

Instead of代替

 <p>{title}</p>

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

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