javascript - Pass reference of a component to another one in ReactJS - Stack Overflow

admin2025-04-19  0

Before anyone press eagerly the close button, I already have looked the following question: ReactJS Two ponents municating. My problem is exactly the third scenario developped in the current accepted answer.

I am using ReactJS to build something with two ponents. For HTML reasons (and presentation), i want my two ponents to be at two different places of the page.

For the moment, I have the following pattern, corresponding to scenario #2:

FooForm = React.createClass({
    ...
});

FooList = React.createClass({
    ...
});

FooManager = React.createClass({
    ...

    render: function () {
        return (
            <div>
                <FooForm ref="form" manager={this} />
                <FooList ref="list" />
            </div>
        );
    }
});

React.render(
    <FooManager someProp={value} />,
    document.getElementById('foo')
);

This gives something like:

<div id="foo">
     <form>Form generated with the render of FooForm</form>
     <ul>List generated with the render of FooList</ul>
</div>

However, i would like to have something like this:

<div id="fooform">
     <form>Form generated with the render of FooForm</form>
</div>
<!-- Some HTML + other controls. Whatever I want in fact -->
<div>...</div>
<div id="foolist">
    <ul>List generated with the render of FooList</ul>
</div>

The problem here is: how can I keep a reference in each ponent? Or at least the link Form -> List?

I tried to create the FooList before and pass the reference to the current manager, but I get the following warning/error:

Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a ponent that doesn't have an owner (that is, was not created inside of another ponent's `render` method). Try rendering this ponent inside of a new top-level ponent which will hold the ref.

The documentation says you can attach events to link two ponents which do not have a parent-child relation. But I don't see how. Can someone give me some pointers?

Before anyone press eagerly the close button, I already have looked the following question: ReactJS Two ponents municating. My problem is exactly the third scenario developped in the current accepted answer.

I am using ReactJS to build something with two ponents. For HTML reasons (and presentation), i want my two ponents to be at two different places of the page.

For the moment, I have the following pattern, corresponding to scenario #2:

FooForm = React.createClass({
    ...
});

FooList = React.createClass({
    ...
});

FooManager = React.createClass({
    ...

    render: function () {
        return (
            <div>
                <FooForm ref="form" manager={this} />
                <FooList ref="list" />
            </div>
        );
    }
});

React.render(
    <FooManager someProp={value} />,
    document.getElementById('foo')
);

This gives something like:

<div id="foo">
     <form>Form generated with the render of FooForm</form>
     <ul>List generated with the render of FooList</ul>
</div>

However, i would like to have something like this:

<div id="fooform">
     <form>Form generated with the render of FooForm</form>
</div>
<!-- Some HTML + other controls. Whatever I want in fact -->
<div>...</div>
<div id="foolist">
    <ul>List generated with the render of FooList</ul>
</div>

The problem here is: how can I keep a reference in each ponent? Or at least the link Form -> List?

I tried to create the FooList before and pass the reference to the current manager, but I get the following warning/error:

Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a ponent that doesn't have an owner (that is, was not created inside of another ponent's `render` method). Try rendering this ponent inside of a new top-level ponent which will hold the ref.

The documentation says you can attach events to link two ponents which do not have a parent-child relation. But I don't see how. Can someone give me some pointers?

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Apr 8, 2015 at 9:03 Maxime LorantMaxime Lorant 36.3k19 gold badges89 silver badges97 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The Less Simple Communication lesson from react-training has a good example of how you can move actions & state sideways to avoid having to create an explicit link between related ponents.

You don't need to jump into a full Flux implementation to get the benefit of this approach, but it's a good example to lead you up to Flux, should you eventually need it or something like it.

Note that this requires you to model the relationship between the ponents based on changing state rather than explicitly passing a reference to a ponent instance (as you're doing above) or a callback bound to the ponent managing the state.

This would be the perfect use-case for a Flux type architecture.

What you want is someone FooManager to be able to trigger state changes in both ponents. Or, in fact, having the different ponents trigger, through Actions, state changes in each other.

The Flux Todo-App Tutorial illustrates your use-case perfectly!

After this, then you'd have the choices of using Facebooks implementation of Flux or the other gazillion ones. My personal favorite is Reflux

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745078121a283690.html

最新回复(0)