A-frame-react : how to pass the value when .map()?

236 Views Asked by At

I would like an Entity to spawn for each item added dynamically in the array.

Is it possible to do like this? How would I attribute the value of each item to each Entity ?

this.state = {
    items: []
}

then in the render :

    <Scene>
      {this.state.items.map((item, key) => {
        return (
            <Entity />
         )
        }
       )}
     </Scene>
1

There are 1 best solutions below

0
Tholle On BEST ANSWER

You can map over the items in your component state and give the item string to the text prop of the Entity component.

Example

class VRScene extends React.Component {
  state = {
    items: ["lemon", "orange"]
  };

  componentDidMount() {
    setInterval(() => {
      this.setState(prevState => {
        return { items: [...prevState.items, Math.random()] };
      });
    }, 2000);
  }

  render() {
    return (
      <Scene>
        {this.state.items.map((item, index) => (
          <Entity
            key={item}
            text={{ value: item, align: "center" }}
            position={{ x: 0, y: 2 - index * 0.1, z: -1 }}
          />
        ))}
      </Scene>
    );
  }
}