react icon indicating copy to clipboard operation
react copied to clipboard

isMounted usage before renderComponent

Open magalhas opened this issue 12 years ago • 7 comments

The usage of isMounted before React.renderComponent results on an invalid access. Of all methods, this one is often used outside of the component to guard access to other methods. Is it really an anti-pattern like described in https://gist.github.com/sebmarkbage/8928067 ? I understand when it comes to other methods, but not in the isMounted case.

magalhas avatar Mar 02 '14 04:03 magalhas

var comp = <comp />; comp.isMounted()

If that's what you are doing then "you're doing it wrong", React promoting "virtual components" to "instances" is an implementation detail in my opinion, don't rely on it because it may change.

Also, what that gist refers to is that methods defined on a component will no longer be available on the class, you need to use statics for that.

syranide avatar Mar 02 '14 20:03 syranide

Is there any any issue describing the reasons behind this decisions so I can take a better grasp on it?

magalhas avatar Mar 02 '14 22:03 magalhas

<child /> and this.refs.child (or this inside a component) are two very different things.

<child /> creates a "virtual component", if you will, basically a shell containing only the class and its props. This tells React what you want the DOM look like after the rendering it done. That is, you return a verbal description of the expected output, not the physical instances.

this.refs.child and this are instances to components that React has created, that physically exist, basically.

So think of <child /> as {type: 'child', props: ...} and not as doing new Child().

syranide avatar Mar 02 '14 23:03 syranide

Thank you for the clarification, it was kind of what I was assuming, but for instance if I can only use the isMounted method inside the component, I can conclude it'll only be used inside the component's life cycle (between componentDidMount and componentWillUnmount) which means that isMounted() will always return true. What's the point on having the method then?

magalhas avatar Mar 03 '14 04:03 magalhas

After a component is unmounted, it's possible its instance will still be accessible if a reference was saved to it, in which case isMounted() will return false.

sophiebits avatar Mar 03 '14 04:03 sophiebits

:+1:

magalhas avatar Mar 03 '14 05:03 magalhas

Hello!,

I also have encountered the warning while trying to access this.props.<component>.refs (looved the link to the gist directly on the console, and the detailed explanation on there makes coding with react really enjoyable).

This is what I was doing.


var Parent = React.createClass({
  componentDidMount : function(){
    if( this.props.aChild.isMounted() ){
      this.props.aChild.refs.doSomething.getDOMNode().focus();
    }
  },
  render : function(){
     return {this.props.aChild};
  }
})

var Child = React.createClass({
  handleFocus : function(){
     // start managing stuff here
  },
  render : function(){
     return (
        <div refs="doSomething" tabIndex={0} onFocus={this.handleFocus}>
            Something <input type="text" placeholder="to do" onChange={this.handleChange} />
        </div>
     );
  }
})

React.renderComponent( Parent({ aChild : Child}), document.body);  

So yes, what I want to do is manage which component is in charge of what is going on. Decide on the parent and pass control to the child. Mainly because convenience of having all contained on each component.

From what I understand, I would have to wait for the child to be mounted to properly access its refs right? I can't think of a better way to do this kind of house keeping and maintain all contained in the same component.

Thoughts?

stringparser avatar Aug 23 '14 11:08 stringparser