I am trying to use React together with X3DOM.
I want to be able to click on the red x3dom <box>
so that it changes its color to blue, when pressed. I have tried using a onClick method in the <shape>
tag. I was only able to do this by pressing a html <button>
instead. I have the button also in the code.
This is my index.js file.
import React from "react";
import ReactDOM from "react-dom";
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = { isToggleOn: true };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
return (
<div>
<x3d width='500px' height='300px' >
<scene>
<shape onClick={this.handleClick}>
<appearance>
<material id='color' diffusecolor={this.state.isToggleOn ? '1 0 0' : '0 0 1'}> </material>
</appearance>
<box></box>
</shape>
</scene>
</x3d>
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'BLUE' : 'RED'}
</button>
</div>
);
}
}
ReactDOM.render(<Toggle />, document.getElementById('toggle'));
Can someone give me a solution, or an explanation why this doesn’t work. I would greatly appreciate any answer.
The problem is that X3DOM calls eventHandlers directly. Quote from the docu:
I have not tested this but adding the onclick once in
componentDidMount
might be the way to go.