gpt4 book ai didi

javascript - React - 未捕获的类型错误 : Cannot read property 'indexOf' of undefined

转载 作者:行者123 更新时间:2023-12-01 03:10:49 25 4
gpt4 key购买 nike

我有以下组件:

import React, { Component } from 'react';

import Utilities from './utilities.js';

import Action from './action.jsx';
import SingleGridEl from './singleGridEl.jsx';

class Grid extends Component {

constructor(props) {
super(props);
this.question;
this.maxN = 110;
this.array = [];

this.state = {
n1: false,
n2: false,
n3: false,
n4: false,
n5: false,
n6: false,
n7: false,
n8: false,
n9: false,
n10: false,
n11: false,
n12: false,
n13: false,
n14: false,
n15: false,
n16: false,
n17: false,
n18: false,
n19: false,
n20: false,
n21: false,
n22: false,
n23: false,
n24: false
}

this.hideSingleGrid = this.hideSingleGrid.bind(this);
}

getRandomQuestion() {
let randomN = Utilities.generateRandomNumber(this.array, this.maxN);
console.log(randomN);
this.question = this.props.current.data.questions[randomN];
console.log(this.question);
return this.question;
}

hideSingleGrid(el, val) {
var s = {};
s[el] = val;
this.setState(s);
}

componentWillMount() {
this.getRandomQuestion();
}

render() {
return (
<section className="game">
<div className="grid">

<div className="row">
<SingleGridEl visibility={this.state.n1} />
<SingleGridEl visibility={this.state.n2} />
<SingleGridEl visibility={this.state.n3} />
<SingleGridEl visibility={this.state.n4} />
<SingleGridEl visibility={this.state.n5} />
<SingleGridEl visibility={this.state.n6} />
</div>
<div className="row">
<SingleGridEl visibility={this.state.n7} />
<SingleGridEl visibility={this.state.n8} />
<SingleGridEl visibility={this.state.n9} />
<SingleGridEl visibility={this.state.n10} />
<SingleGridEl visibility={this.state.n11} />
<SingleGridEl visibility={this.state.n12} />
</div>
<div className="row">
<SingleGridEl visibility={this.state.n13} />
<SingleGridEl visibility={this.state.n14} />
<SingleGridEl visibility={this.state.n15} />
<SingleGridEl visibility={this.state.n16} />
<SingleGridEl visibility={this.state.n17} />
<SingleGridEl visibility={this.state.n18} />
</div>
<div className="row">
<SingleGridEl visibility={this.state.n19} />
<SingleGridEl visibility={this.state.n20} />
<SingleGridEl visibility={this.state.n21} />
<SingleGridEl visibility={this.state.n22} />
<SingleGridEl visibility={this.state.n23} />
<SingleGridEl visibility={this.state.n24} />
</div>
</div>

<Action t={this.hideSingleGrid} question={this.question} getRandomQuestion={this.getRandomQuestion.bind(this)}/>
</section>
);
}
}

export default Grid;

它使用了以下实用程序方法:

var Utilities = (function (array, maxN) {

function generateRandomNumber(array, maxN) {
let randomN = Math.floor((Math.random() * maxN) + 1);

if(array.indexOf(randomN) === -1) {
array.push(randomN);
}
else {
randomN = generateRandomNumber();
}

console.log(array);

return randomN;
}

return {
generateRandomNumber: generateRandomNumber
};

})();

export default Utilities;

此实用程序方法生成一个随机数并将其存储在数组中,以便根据传入的最大长度返回的每个随机数都是唯一的。

action.jsx 组件使用相同的实用方法:

import React, { Component } from 'react';

import Utilities from './utilities.js';

class Action extends Component {
constructor(props) {
super(props);

this.maxN = 24;
this.array = [];

this.state = {
question: props.question
}
}

getNewQuestion(e) {
let answerId = "option_" + this.state.question.correct_option;

if(e.target.getAttribute('data-question') == answerId) {
let randomNumber = Utilities.generateRandomNumber(this.array, this.maxN);
this.props.t('n'+randomNumber, true);
this.setState({
question: this.props.getRandomQuestion()
});
}
else {
console.log('wrong');
}
}

render() {
let state = this.state;

return(
<div className="action">
<div className="action-question">
<h3>{state.question.question}</h3>
</div>

<div className="action-answers">
<p data-question="option_1" onClick={this.getNewQuestion.bind(this)}>{state.question.option_1}</p>
<p data-question="option_2" onClick={this.getNewQuestion.bind(this)}>{state.question.option_2}</p>
<p data-question="option_3" onClick={this.getNewQuestion.bind(this)}>{state.question.option_3}</p>
<p data-question="option_4" onClick={this.getNewQuestion.bind(this)}>{state.question.option_4}</p>
</div>
</div>
);
}
}

export default Action;

当我单击每个“p”时,“this.getNewQuestion”会检查我们是否单击了正确的答案,并转到具有相关答案的下一个问题。

在游戏过程中的某个时刻(非常随机),我在控制台中收到以下错误。错误指的是实用方法:

if(array.indexOf(randomN) === -1) {

enter image description here

我无法弄清楚是什么导致了该错误..

最佳答案

在调用 indexOf 之前,您需要检查您的 array 参数实际上是否是一个数组:

if(Array.isArray(array) && array.indexOf(randomN) === -1) {

}

或提供默认参数

function generateRandomNumber(array = [], maxN) {

代码中的这一行正在调用不带任何参数的函数,并将触发错误:

else {
randomN = generateRandomNumber();
}

关于javascript - React - 未捕获的类型错误 : Cannot read property 'indexOf' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45865521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com