简体   繁体   English

在同一个React组件的不同实例之间切换时使用componentDidMount

[英]Utilizing componentDidMount when switching between different instances of the same React component

According to the react documentation: http://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount we are adviced use componentDidMount for AJAX call that should be issued when a component is brought into view. 根据反应文档: http//facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount我们建议使用componentDidMount进行AJAX调用,当组件进入视图时应该发出。

However, when switching between to instances of the same component with different props, componentDidMount is only called for the first component. 但是,当使用不同的props切换到同一组件的实例时,仅为第一个组件调用componentDidMount。 So what are we supposed to do in this situation? 那么在这种情况下我们应该做些什么呢?

Currently I have the following workaround: In componentDidMount i do my AJAX call and In componentDidUpdate I compare old and new props to check if I am on a new "instance", and if so I do my AJAX call. 目前我有以下解决方法:在componentDidMount中,我执行我的AJAX调用,在componentDidUpdate中,我比较旧的和新的道具,以检查我是否在新的“实例”,如果是,我做我的AJAX调用。 But that seems exactly like a workaround. 但这似乎就像一个解决方法。 So my question is: is this really the way to do it? 所以我的问题是:这真的是这样做的吗?

I am aware that I could wrap my component in different empty components to solve my problem. 我知道我可以将我的组件包装在不同的空组件中以解决我的问题。 However, this is not possible because we are building a data driven application that uses configurable components and it makes sense to use the same component with different configurations - which is where I'm running into problems. 但是,这是不可能的,因为我们正在构建一个使用可配置组件的数据驱动应用程序,并且使用具有不同配置的相同组件是有意义的 - 这是我遇到问题的地方。

I am aware that we are actually talking about react elements and not instances as such - witch I guess is part of the problem. 我知道我们实际上是在讨论反应元素而不是实例 - 我猜这是问题的一部分。 Probably I have different react elements utilizing the same instance. 可能我有不同的反应元素利用相同的实例。

I have made a tiny example to illustrate the react behavior, using plain react (just to make sure I wasn't tricked by react-router or redux and what else we are using the the real app): 我做了一个很小的例子来说明反应行为,使用简单的反应(只是为了确保我没有被react-router或redux欺骗,以及我们使用真正的应用程序还有什么):

class Foo extends React.Component {
    componentDidMount() {
        console.log('componentDidMount ' + this.props.foo);
    }

    componentDidUpdate() {
        console.log('componentDidUpdate ' + this.props.foo);
    }

    render() {
        return <div>Route is {this.props.foo}</div>;
    }
}

function navigated() {
    ReactDOM.render(
        <Foo foo={window.location.hash} />, 
        document.getElementById('app')
    );
}

window.addEventListener('hashchange', navigated, false);
navigated();

Initially when I go to #/bar I get 'componentDidMount #/bar' and when I go to #/baz i get 'componentDidUpdate #/baz'. 最初当我去#/ bar时,我得到'componentDidMount#/ bar',当我去#/ baz时,我得到'componentDidUpdate#/ baz'。

I seems like this unanswered question is a specific case of the same issue: React does not know when i render the same component 我似乎这个未解决的问题是同一问题的特定情况: React不知道我何时渲染相同的组件

You can add the key property with unique value for each of hashes: 您可以为每个哈希添加具有唯一值的key属性:

ReactDOM.render(
    <Component hash={hash} key={hash} />, domNode
);

This will update the component every time when the hash is really changed. 每次哈希真正改变时,这将更新组件。

https://facebook.github.io/react/docs/multiple-components.html#dynamic-children https://facebook.github.io/react/docs/multiple-components.html#dynamic-children

TL DR - your 'workaround' looks correct to me TL DR - 您的“解决方法”对我来说是正确的

When you initially render the component componentDidMount is called, when you change the hash prop componentDidUpdate is called. 当您最初渲染componentDidMount ,调用componentDidMount ,当您更改hash道具时,会调用componentDidUpdate It is still the same component, it is just that a specific prop has changed value. 它仍然是同一个组件,它只是一个特定的道具改变了价值。 In your case, you have logic (running an AJAX call when hash changes) that is specific to your application. 在您的情况下,您具有特定于您的应用程序的逻辑(在哈希更改时运行AJAX调用)。 React does not known that the hash prop is special, you make is special by adding the logic in componentDidMount . React不知道hash prop是特殊的,你通过在componentDidMount添加逻辑来使其特殊。 So I believe you have a good interpretation of the React docs and this way of achieving your goal is perfectly valid. 因此,我相信您对React文档有一个很好的解释,这种实现目标的方式是完全有效的。

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

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