当前位置: 首页 > 面试题库 >

在componentDidMount()之后停止呈现组件

尉迟晔
2023-03-14
问题内容

我有一个包含三个部分的搜索页面。浏览主题组件列出了可供选择的主题。浏览文章组件基于主题ID列出所有文章,如果没有主题ID,则加载所有文章。home组件包含browsertopics和browserarticles组件,并根据所单击的主题更改其状态。

class BrowseTopics extends React.Component {
  constructor(props) {
    super(props);
    this.topicSelect = this.topicSelect.bind(this);
    this.state = {error: "", topics: []};
  }
  componentDidMount(){
    // API call which updates state topics with the list of topics
  }
  topicSelect(id,e) {
    e.preventDefault();
    this.props.topicChange(id);
  }
 render () {
    // Rendering list of topics from API and nothing if request has not been sent
  }
}

class BrowseArticles extends React.Component {
  constructor(props) {
    super(props);
    this.state = {error: "", articles: [], url: "/api/articles"};
  }
  componentDidMount() {
    if(this.props.topicId){
    var url = '/api/topic/'+this.props.topicId+'/articles';
    this.setState({url: url});
    }
    // Make a request to url and get articles
  }
  render () {
    // Renders the list of articles
  }
}

class Home extends React.Component {
  constructor(props) {
    super(props);
    this.handleUpdate = this.handleUpdate.bind(this);
    this.state = {topicId: ""};
  }

  handleUpdate(topicId) {
    this.setState({topicId: topicId});
  }

  render () {

    return(
<div>
<BrowseTopics user={this.props.user} topicChange={this.handleUpdate}/>
          <BrowseArticles user={this.props.user} topicId={this.state.topicId}/>
</div>
      );
  }
}

我需要的是,我希望BrowseTopics组件在父状态更改时停止重新呈现。我尝试使用shouldComponentUpdate()(返回false),但它甚至停止了componentDidMount()部分,并且列表中没有填充。

发出对API的请求并呈现了组件后,我希望所有进一步的browserTopics重新呈现都停止,以使排序正常运行。


问题答案:

从文档:

如果shouldComponentUpdate()返回false,那么componentWillUpdate()render()componentDidUpdate()将不被调用

我可能想要设置某种标志来告诉我的BrowseTopics组件已经发出了API请求,而我不再需要/想要该组件进行更新:

class BrowseTopics extends React.Component {
  constructor(props) {
    super(props);
    this.topicSelect = this.topicSelect.bind(this);
    this.state = {
      error: "",
      topics: [],
      hasFetched: false // flag for API
    };
  }
  componentDidMount(){
    // API call which updates state topics with the list of topics
    fetch( 'myapi.json' )
      .then( res => {
        // set flag denoting API results have been fetcehd
        this.setState({
          hasFetched: true,
          topics: <your topics>
        });
      })
  }

  shouldComponentUpdate(nextProps, nextState) {
    if ( this.state.hasFetched ) {
      return false;
    }
    return true;
  }
  ...


 类似资料:
  • 我是ReactJS的新手,我现在对组件生命周期方法的工作方式感到困惑;在我的例子中,这与ReactJS. org文档完全矛盾。预期的行为是在渲染()之前执行,但是渲染()在组件迪蒙()之前执行。 我的源代码: 类变量this.cache是如果被调用在,但它占用的值如果在中调用。

  • 但是当我从ComponentOne切换到ComponentTwo,然后回到ComponentOne时,它被重新构建,我失去了在ComponentOne中已经做过的一切 是否有一种方法,当创建的组件没有呈现时,它不会丢失其所有状态?

  • 基本上,我有一个非常简单的react组件。它所做的是,环绕“反应-对讲机”,只有当状态发生变化时才会呈现它。为了简化这个问题,我硬连接了方法以始终返回false。 发生的是它总是重新发送,这不应该发生。 有人知道为什么会这样吗?

  • 我在一个表单上有一个TextField输入,问题是当我在它上键入时,刚刚键入的值的显示/呈现有一种延迟,我想防止这种延迟。 代码:

  • 我的目标是有两个不同的FullCalendar元素保持同步。我不做任何网络调用,在这一点上只是客户端数组。 当我添加一个事件时(比如通过点击或者在它构建之后),日历实际上不会像文档所说的那样重新呈现。 我还尝试使用“renderEvents”和“renderEvent”风格,但这似乎没有必要,因为“updateEvents”将/应该更新这些事件的呈现。我还遇到了一个问题,日历列表版本上的“rend

  • 问题内容: 我父母的渲染中有以下代码 以及下面我孩子图表中的代码 我以为仅在所有子项都加载后才调用父级的componentDidMount。但是在这里,父级的componentDidMount在子级的componentDidMount之前被调用。 这是工作方式吗?还是我做错了什么。 如果这是工作方式,我如何检测从父级加载所有子级组件的时间? 问题答案: 是的,孩子的称为父母之前的。 运行下面的代码