Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Rendering a <line /> with updated state. #213

Open
tmulroy opened this issue Feb 5, 2018 · 3 comments
Open

Rendering a <line /> with updated state. #213

tmulroy opened this issue Feb 5, 2018 · 3 comments

Comments

@tmulroy
Copy link

tmulroy commented Feb 5, 2018

I'm having trouble understanding why this isn't work. On componentWillReceiveProps, I update the vertices in the state, which I use to render a line. However, when I pass in the new vertices as a prop, the state updates, but the new line isn't created. I'm not getting any errors. I'm brand new to react-three-renderer, so it's most likely something I'm doing wrong.

import React, { Component } from 'react';
import React3 from 'react-three-renderer';
import { StyleSheet, css } from 'aphrodite';
import * as THREE from 'three';
import ReactDOM from 'react-dom';

export default class Arm extends Component {
  constructor(props, context) {
    super(props, context);
    this.cameraPosition = new THREE.Vector3(0, 0, 5);
    this.state = {
      vertices: [
        new THREE.Vector3( 0, 0, 0 ),
        new THREE.Vector3(1,0,0),
      ]
    }
  }

  componentWillReceiveProps(nextProps) {
    let nextVertices = [];
    let x, y, z;
    for (let i=0; i < nextProps.jointCoordinates.length; i++) {
      x = nextProps.jointCoordinates[i][0];
      y = nextProps.jointCoordinates[i][1];
      z = nextProps.jointCoordinates[i][2];
      nextVertices.push(new THREE.Vector3(x,y,z))
    }
    this.setState({vertices: nextVertices})
  }

  render() {
    //console.log('arm state', this.state);
    const width = 0.98*(window.innerWidth);
    const height = 0.681*(window.innerHeight);

    return (
      <React3
        mainCamera="camera"
        clearColor='#f8f8ff'
        width={width}
        height={height}
    >
      <scene>
        <perspectiveCamera
          name="camera"
          fov={75}
          aspect={width / height}
          near={0.1}
          far={1000}
          position={this.cameraPosition}
        />
        <line>
          <geometry vertices={this.state.vertices} />
          <lineBasicMaterial color={0xff2500} />
        </line>
      </scene>
    </React3>);
  }
}
@toxicFork
Copy link
Collaborator

toxicFork commented Feb 5, 2018 via email

@tmulroy
Copy link
Author

tmulroy commented Feb 7, 2018

Hi @toxicFork thanks for responding! This is the same as issue #131 I used the workaround by adding a key to the line element. Any idea where I should start looking in the code so I can help with a more permanent solution/fix?

@toxicFork
Copy link
Collaborator

toxicFork commented Feb 8, 2018 via email

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

No branches or pull requests

2 participants