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

React -超出了最大调用堆栈大小

颛孙飞鸾
2023-03-14

我正在使用我岳父让我建立的一个网站作为学习React的借口。我不是一个天生的JavaScript开发人员(更喜欢后端),我知道我下面的代码中可能有一些递归元素,但我就是看不到。谁能告诉我我做错了什么?

import React, { Component } from 'react';
import { Container, Segment, Grid, Image, Card, Divider, Header } from 'semantic-ui-react';
import publicationData from '../data/publicationData';

const Publication = (props) => (
  <Card>
      <Image src={props.img_url} style={{ height: "350px", width: "auto", margin: "15px auto"}}/>
      <Card.Content style={{textAlign: "center"}}>
        <Card.Header >{props.title}</Card.Header>
      </Card.Content>
  </Card>
);



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

        this.state = { books: publicationData, currentBook: publicationData[0] };
        this.changeCurrentBook.bind(this);

    }
    changeCurrentBook(e) {
        this.setState({ currentBook: e });
    }
  render() {
    return(
            <Segment basic>
                <Container>
                    <Grid stackable>
                        <Grid.Row divided>
                            <Grid.Column width={8}>
                                <Image src={ this.state.currentBook.img_url } centered/>
                            </Grid.Column>
                            <Grid.Column width={8} verticalAlign="middle">
                                <Header content={ this.state.currentBook.title} />
                                <p>{ this.state.currentBook.blurb }</p>
                            </Grid.Column>
                        </Grid.Row>
                    </Grid>
                    <Grid.Row>
                    <Divider />
                    <Grid.Column>
                        <Card.Group itemsPerRow={4} doubling stackable>
                            { this.state.books.map((publication) => {
                                  return (
                                    <Publication 
                                        title={publication.title}
                                      blurb={publication.blurb}
                                      img_url={publication.img_url}
                                      key={publication.title}
                                      onClick={ this.changeCurrentBook(publication) }
                                    />
                                  )
                                }) 
                            }
                        </Card.Group>
                    </Grid.Column>
                    </Grid.Row>
                </Container>
            </Segment>
        );
    }
}

export default Publications;

共有2个答案

魏宸
2023-03-14
匿名用户

像这样编写< code>Publication组件的< code>onClick

<Publication 
   title={publication.title}
   blurb={publication.blurb}
   img_url={publication.img_url}
   key={publication.title}
   onClick={ () => this.changeCurrentBook(publication) }
/>

你还需要像这样绑定你的方法

constructor(props) {
  super(props);
  this.state = { books: publicationData, currentBook: publicationData[0] };
  this.changeCurrentBook = this.changeCurrentBook.bind(this);
}

拓拔欣嘉
2023-03-14

如果你写

onClick={ this.changeCurrentBook(publication) } 

函数将立即执行,计算值将传递给单击时的。您需要的是运行一个代码,该代码会立即返回一个函数,该函数将在单击时调用。为此,请从

onClick={ this.changeCurrentBook(publication) } 

onClick = {() => this.changeCurrentBook(publication)}

作为进一步的改进,还值得注意的是,在渲染中编写箭头函数会对性能产生一些影响。事实上,每次渲染Publation组件时,它都会生成一个新的相同的onClick函数,这反过来又会强制组件重新渲染。

为了避免额外的无用渲染,让我们将处理程序更改为返回一个函数,该函数将在单击时调用

changeCurrentBook(e) {
  return function() { 
    this.setState({ currentBook: e }); 
  }  
}

这样,我们就不需要渲染方法中的任何箭头函数,也不会有任何额外的渲染:

 onClick={ this.changeCurrentBook(publication) }

有关进一步的说明,请参阅此处。

 类似资料:
  • 如果用户未登录,我尝试将用户重定向到“TrapPage”。 这是我的代码: 当我将函数requireAuth放在onEnter上时,控制台给我一个错误: 我是一个反应迟钝的人,请耐心点:) 我的代码有什么问题?

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

  • 这是我在React中的第一个应用程序。在localhost中,一切正常,当我使用Github Pages部署时,我的应用程序(Info/Images/evenements)中的一些页面无法呈现。每次单击他们的链接访问他们时,我都会在控制台上看到一个白色页面和此错误: range error:object . tostring处超出了最大调用堆栈大小 同样,当我刷新页面时,github pages返

  • 当我添加<代码>时 我得到错误 超过了最大调用堆栈大小 当然,如果我注释掉

  • 当我为图表(react-chartjs-2)获取数据时,我收到了这个错误。我实现了try/catch块,但不知道如何删除这个错误。在Edge浏览器上,我得到了CRIPT28:SCRIPT28:堆栈空间不足 这是我在组件中调用的操作。 这是连接到redux存储的组件。它在我看来一切都很好。

  • 我正在尝试将文档批量插入MongoDB(因此绕过Mongoose并使用本机驱动程序,因为Mongoose不支持批量插入文档数组)。我这样做的原因是为了提高写作速度。 我收到错误RangeError: Maximum Call Stack Size Exceeded在console.log(err)在下面的代码: 类似于:https://stackoverflow.com/questions/243