当前位置: 首页 > 知识库问答 >
问题:

范围错误: 最大调用堆栈大小超过了 Reactjs?

董翰池
2023-03-14

索引.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Questionlist from './quiz/Questionlist.jsx';
import Scorebox from './quiz/Scorebox.jsx';
import Results from './quiz/Results.jsx';
import * as serviceWorker from './serviceWorker';

class Quiz extends React.Component {
    constructor(props){
        super(props);
            this.state= {
                questions : [
                    {
                        id: 1,
                        text: 'What is your name?',
                        choices:[
                            {
                                id: 'a',
                                text: 'Michael'
                            },
                            {
                                id: 'b',
                                text: 'Brand'
                            },
                            {
                                id: 'c',
                                text: 'Steven'
                            },
                        ],
                        correct: 'b'
                    },
                    {
                        id: 2,
                        text: 'What is your mother name?',
                        choices:[
                            {
                                id: 'a',
                                text: 'Sara'
                            },
                            {
                                id: 'b',
                                text: 'Denny'
                            },
                            {
                                id: 'c',
                                text: 'senny'
                            },
                        ],
                        correct: 'c'
                    },
                    {
                        id: 3,
                        text: 'What is your father name?',
                        choices:[
                            {
                                id: 'a',
                                text: 'Bobby'
                            },
                            {
                                id: 'b',
                                text: 'Harry'
                            },
                            {
                                id: 'c',
                                text: 'Waye'
                            },
                        ],
                        correct: 'c'
                    },
                    {
                        id: 4,
                        text: 'What is your friend name?',
                        choices:[
                            {
                                id: 'a',
                                text: 'John'
                            },
                            {
                                id: 'b',
                                text: 'Paul'
                            },
                            {
                                id: 'c',
                                text: 'Jose'
                            },
                        ],
                        correct: 'a'
                    },
                ],
                score: 0,
                current: 1
            }
    }

    setCurrent(current){
        this.setState({current});
    }
    setScore(score){
        this.setScore({score});
    }

    render() {
        if(this.state.current > this.state.questions.length){
            var scorebox = '';
            var results = <Results {...this.state} />
        }else{
            scorebox = <Scorebox {...this.state} />
            var results = '';
        }
        //return <Questionlist questions={this.state.questions} />
        return(
            <div className="container">
                <div className="row">
                    <div className="col-md-12">
                        <h1>Quiz</h1>
                        {scorebox}      
                        <Questionlist {...this.state} setCurrent={this.setCurrent.bind(this)} setScore={this.setScore.bind(this)} />
                        {results}
                    </div>
                </div>
            </div>
        )
    }
}


ReactDOM.render(<Quiz  />, document.getElementById('root'));
serviceWorker.unregister();

Questionlist.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import Question from './Question.jsx';

class Questionlist extends React.Component {

    render() {
        return(
            <div className="question">
                {
                    this.props.questions.map(question => {
                        //console.log(question.id);
                        //console.log(this.props.current);
                        if(question.id == this.props.current){
                            return <Question question={question} key={question.id} {...this.props}  />
                        }
                        
                    })
                }
            </div>
        )
    }
}

export default Questionlist

问题.jsx

import React from 'react';
import ReactDOM from 'react-dom';

class Question extends React.Component {
    onChange(e){
        e.preventDefault();
        const {setCurrent, setScore, question} = this.props;

        let selected = e.target.value;

        if(selected == question.correct){
            setScore(this.props.score+1);
        }

        setCurrent(this.props.current+1)
    }
    render() {
        const {question} = this.props;
        return(
            <div className="well">
                <h3>{question.text}</h3>
                <hr />
                <ul className="list-group">
                    {
                        this.props.question.choices.map(choice => {
                            return(
                                <li className="list-group-item" key={choice.id}>
                                    {choice.id} <input type="radio" onChange={this.onChange.bind(this)} name={question.id} value={choice.id} /> {choice.text}
                                </li>
                            )
                        })
                    }
                </ul>
            </div>
        )
    }
}

export default Question

Scorebox.jsx

import React from 'react';
import ReactDOM from 'react-dom';

class Scorebox extends React.Component {

    render() {
        console.log(this.props);
        return(
            <div className="well">
                Question {this.props.current} out of {this.props.questions.length} <span className="pull-right"><strong>Score: {this.props.score}</strong></span>
            </div>
        )
    }
}

export default Scorebox

结果. jsx

import React from 'react';
import ReactDOM from 'react-dom';

class Results extends React.Component {

    render() {
        var percent = (this.props.score / this.props.questions.length * 100);
        if(percent > 80){
            var message = "Awesome";
        }else if(percent < 80 && percent > 60){
            var message = "You did ok";
        }else{
            var message = "You did Harriable";
        }
        return(
            <div className="well">
                <h4>You Got {this.props.score} out of {this.props.questions.length} Correct</h4>
                <h1>{percent}% - {message}</h1>
                <hr />
                <a href="/app">Take Again</a>
            </div>
        )
    }
}

export default Results

共有1个答案

呼延渝
2023-03-14

索引中的此方法.js将导致无限循环,当您在问题组件❌的 onChange 事件上调用此方法时

setScore(score){
    this.setScore({score});
}

你想要的是设置新状态,而不是重新调用方法✅

setScore(score){
    this.setState({score});
}
 类似资料:
  • 我刚刚开始使用角度2。所以我尝试使用Web服务从数据库显示类别。 这是我的论坛.服务.ts文件 forum.component.ts: forum.component.html: 请帮忙,先谢了

  • 我在react中工作,基本上我想做一个带工具提示的按钮,现在我正在做工具提示。我正在更改css显示属性,以使它在鼠标进入和离开时可见或不可见。但是有一个错误,我不知道该怎么办... 这是我的代码: 在控制台中,我收到了这个错误: 我找不到问题出在哪里。我知道这可能是关于调用一个函数,而这个函数又调用另一个函数。但是我在代码中看不到这样的东西,我不确定是否都是这样。谢谢帮助:)

  • //{this.props.params.item}来自反应路由器(路径('/detail/item/id')) 为什么我的调度是无限循环,直到出错(超过最大调用堆栈大小)

  • 我在玩React,我得到了我想要的功能,但是由于某个地方的无限循环,它非常慢。我相信它在组件生命周期方法中,但我不知道如何重新格式化下面的代码,使其具有相同的功能,但没有无限循环。任何关于最佳实践的建议都将不胜感激。

  • 我是React的初学者。js并试图理解道具的概念 我有两个组件 - UserData.jsx UserDataResult.jsx 在UserData中。jsx我用过这个。用户记录的状态。我想通过子组件UserDataResult.jsx显示此记录。 不知道我做错了什么,所以我在控制台日志中得到错误。 错误是:未捕获的范围错误:超出最大调用堆栈大小 代码: **UserData.jsx** **用

  • 下面的代码抛出了问题标题中提到的堆栈溢出。我正试图让一个方框阴影以脉冲效果显示在一个圆形图像的周围。有人能指出递归吗?我是一个Javascript新手,看不出来。谢谢你。 断续器 CSS 脚本