Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some components cannot be used until React Component is mounted #52

Closed
jsantell opened this issue Dec 29, 2016 · 6 comments
Closed

Some components cannot be used until React Component is mounted #52

jsantell opened this issue Dec 29, 2016 · 6 comments
Labels

Comments

@jsantell
Copy link
Contributor

When adding a raycaster and cursor component (to represent an <a-cursor>) to an entity, the element gets created before mounted to the DOM, causing the raycaster component's this.el.sceneEl to be undefined (as it has yet to be mounted), throwing an error and causing the scene to fail to render. A work around is to wait until the component is mounted to DOM and then adding more components, but this took some time to figure out. Similar to assumptions in #50 of when things are mounted and initialized, I wonder if there's a general way of handling this for all components.

import React, { Component, PropTypes } from 'react';
import { Entity } from 'aframe-react';

class CameraComponent extends Component {
  constructor() {
    super();
    this.state = {
      mounted: false,
    };
  }

  componentDidMount() {
    this.setState({ mounted: true });
  }

  render() {
    const mountedProps = {};

    if (this.state.mounted) {
      mountedProps.cursor = 'maxDistance: 100; fuse: true; fuseTimeout: 1000'
    };

    return (
      <Entity id='camera-container' {...this.props}>
        <Entity camera>
          <Entity
            {...mountedProps}
            raycaster='objects: .action'    
            geometry='primitive: plane'>
          </Entity>
        </Entity>
      </Entity>
    );
  }
};

export default CameraComponent;
@ngokevin
Copy link
Collaborator

That's weird since A-Frame won't try to execute the components until the entity is mounted.

@jsantell
Copy link
Contributor Author

This is also after the a-scene's loading event is fired, if it helps

@ngokevin
Copy link
Collaborator

OK thanks, got a lot of maintenance lined up for aframe-react :)

@jsantell
Copy link
Contributor Author

jsantell commented Dec 29, 2016

Looks like an aframe issue, filed aframevr/aframe#2225

Looks like I was wrong about that bug being related here, that example does not inject the component within an a-scene

@ngokevin
Copy link
Collaborator

v4.0.x should solve since .setAttribute()s are called on the ref, which is after mount. Will add unit tests though.

@ngokevin
Copy link
Collaborator

Added unit test for wrapped entity. 44bee27

Didn't see any errors in the log. Should work now since everything is initialized only once after the ref is attained. In other words, after mount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants