admin管理员组文章数量:1278826
I'm making a Simon game, where there are 4 quarter circles all with class name 'colorButton': red, yellow blue and green.
I'd like it so that when it's the puter turn, all pointer events to the 4 colored circles are disabled so you can't click on them.
In my code i use:
const colorButtons = document.getElementsByClassName('colorButton');
colorButtons.style.pointerEvents = 'none';
But I get this console error:
Uncaught TypeError: Cannot set property 'pointerEvents' of undefined
at AppputerMove (http://localhost:8080/bundle.js:14010:42)
at App.startGame (http://localhost:8080/bundle.js:13997:14)
Am I doing something wrong here?
Entire code for reference:
import React, { Component } from 'react';
import ColorButton from './colorbutton';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
powerOn: false,
start: false,
myTurn: false,
pMoves: ['red', 'yellow', 'blue', 'green'],
myMoves: [],
count: null
};
this.colors = ['green', 'red', 'yellow', 'blue'];
this.powerOn = this.powerOn.bind(this);
this.startGame = this.startGame.bind(this);
this.highlightBtn = this.highlightBtn.bind(this);
thisputerMove = thisputerMove.bind(this);
}
startGame() {
const { powerOn } = this.state;
if (powerOn) {
this.setState({ start: true });
thisputerMove();
}
}
puterMove(){
if (!this.state.myTurn) {
const randNum = Math.floor(Math.random() * 4);
const randColor = this.colors[randNum];
const count = this.state.count;
const colorButtons = document.getElementsByClassName('colorButton');
colorButtons.style.pointerEvents = 'none';
const pMoves = this.statepMoves.slice();
pMoves.push(randColor);
var i=0;
const repeatMoves = setInterval(() => {
this.highlightBtn(pMoves[i]);
i++;
if (i >= pMoves.length) {
clearInterval(repeatMoves);
}
}, 1000);
this.setState({
pMoves: pMoves,
myTurn: true,
count: count + 1
});
}
}
highlightBtn(color) {
const audio = document.getElementById(color+'Sound');
audio.play();
const selectColor = document.getElementById(color);
selectColor.style.opacity = 0.5;
setTimeout(() =>{ selectColor.style.opacity = 1}, 200);
}
powerOn(event) {
const { powerOn } = this.state;
if (!powerOn) { this.setState({ powerOn: true }) }
else { this.setState({ powerOn: false }) }
}
render() {
console.log('moves:', this.statepMoves);
const { count } = this.state;
return(
<div className='container'>
<div className='outer-circle'>
<ColorButton
color='green'
handleClick={() => this.highlightBtn('green')}
/>
<ColorButton
color='red'
handleClick={() => this.highlightBtn('red')}
/>
<ColorButton
color='yellow'
handleClick={() => this.highlightBtn('yellow')}
/>
<ColorButton
color='blue'
handleClick={() => this.highlightBtn('blue')}
/>
<div className='inner-circle'>
<h2>Simon®</h2>
<div className='count-box'>
<div className='count-display'>{count ? count : '--'}</div>
<div className='small-text'>Count</div>
</div>
</div>
</div>
<div className='controls'>
<div className='power-box'>
<div className='power-text'>OFF</div>
<label className="switch">
<input type="checkbox" onChange={this.powerOn}/>
<div className="slider round"></div>
</label>
<div className='power-text'>ON</div>
</div>
<div className='buttons'>
<div className='start-box'>
<div className='start-button' onClick={this.startGame}></div>
<div className='small-text'>Start</div>
</div>
<div className='strict-box'>
<div className='strict-button'></div>
<div className='small-text'>Strict</div>
</div>
</div>
</div>
<p className='footer'>
Source code on <a href=''>Github</a>
</p>
</div>
)
}
}
I'm making a Simon game, where there are 4 quarter circles all with class name 'colorButton': red, yellow blue and green.
I'd like it so that when it's the puter turn, all pointer events to the 4 colored circles are disabled so you can't click on them.
In my code i use:
const colorButtons = document.getElementsByClassName('colorButton');
colorButtons.style.pointerEvents = 'none';
But I get this console error:
Uncaught TypeError: Cannot set property 'pointerEvents' of undefined
at App.puterMove (http://localhost:8080/bundle.js:14010:42)
at App.startGame (http://localhost:8080/bundle.js:13997:14)
Am I doing something wrong here?
Entire code for reference:
import React, { Component } from 'react';
import ColorButton from './colorbutton';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
powerOn: false,
start: false,
myTurn: false,
pMoves: ['red', 'yellow', 'blue', 'green'],
myMoves: [],
count: null
};
this.colors = ['green', 'red', 'yellow', 'blue'];
this.powerOn = this.powerOn.bind(this);
this.startGame = this.startGame.bind(this);
this.highlightBtn = this.highlightBtn.bind(this);
this.puterMove = this.puterMove.bind(this);
}
startGame() {
const { powerOn } = this.state;
if (powerOn) {
this.setState({ start: true });
this.puterMove();
}
}
puterMove(){
if (!this.state.myTurn) {
const randNum = Math.floor(Math.random() * 4);
const randColor = this.colors[randNum];
const count = this.state.count;
const colorButtons = document.getElementsByClassName('colorButton');
colorButtons.style.pointerEvents = 'none';
const pMoves = this.state.pMoves.slice();
pMoves.push(randColor);
var i=0;
const repeatMoves = setInterval(() => {
this.highlightBtn(pMoves[i]);
i++;
if (i >= pMoves.length) {
clearInterval(repeatMoves);
}
}, 1000);
this.setState({
pMoves: pMoves,
myTurn: true,
count: count + 1
});
}
}
highlightBtn(color) {
const audio = document.getElementById(color+'Sound');
audio.play();
const selectColor = document.getElementById(color);
selectColor.style.opacity = 0.5;
setTimeout(() =>{ selectColor.style.opacity = 1}, 200);
}
powerOn(event) {
const { powerOn } = this.state;
if (!powerOn) { this.setState({ powerOn: true }) }
else { this.setState({ powerOn: false }) }
}
render() {
console.log('moves:', this.state.pMoves);
const { count } = this.state;
return(
<div className='container'>
<div className='outer-circle'>
<ColorButton
color='green'
handleClick={() => this.highlightBtn('green')}
/>
<ColorButton
color='red'
handleClick={() => this.highlightBtn('red')}
/>
<ColorButton
color='yellow'
handleClick={() => this.highlightBtn('yellow')}
/>
<ColorButton
color='blue'
handleClick={() => this.highlightBtn('blue')}
/>
<div className='inner-circle'>
<h2>Simon®</h2>
<div className='count-box'>
<div className='count-display'>{count ? count : '--'}</div>
<div className='small-text'>Count</div>
</div>
</div>
</div>
<div className='controls'>
<div className='power-box'>
<div className='power-text'>OFF</div>
<label className="switch">
<input type="checkbox" onChange={this.powerOn}/>
<div className="slider round"></div>
</label>
<div className='power-text'>ON</div>
</div>
<div className='buttons'>
<div className='start-box'>
<div className='start-button' onClick={this.startGame}></div>
<div className='small-text'>Start</div>
</div>
<div className='strict-box'>
<div className='strict-button'></div>
<div className='small-text'>Strict</div>
</div>
</div>
</div>
<p className='footer'>
Source code on <a href='https://github./drhectapus/react-simon-game'>Github</a>
</p>
</div>
)
}
}
Share
Improve this question
asked May 26, 2017 at 23:38
doctopusdoctopus
5,65713 gold badges65 silver badges118 bronze badges
1 Answer
Reset to default 8The cause of your error is that getElementsByClassName returns an array-like object. Thus, it does not have a style property. Hence, colorButtons.style is undefined and colorButtons.style.pointerEvents causes your error.
I'd also point out though that your general approach is pretty un-react like. You almost never want to mutate the DOM directly like this. With React, you are just defining how a ponent should render given props and state. One big problem with mutating the DOM is that your changes are going to get wiped away every time a re-render is triggered. I would look into handling what you're trying to do with something like this:
<ColorButton
color='green'
disabled={ !this.state.myTurn }
handleClick={() => this.highlightBtn('green')}
/>
class ColorButton extends Component {
render() {
return (
<div style={{ pointerEvents: this.props.disabled ? 'none' : 'auto' }}>
...
</div>
)
}
}
本文标签: javascriptCan39t disable pointerevents in React appStack Overflow
版权声明:本文标题:javascript - Can't disable pointer-events in React app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741299306a2371000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论