我正在使用一个网站,我的岳父让我建立作为学习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;
你可以请加功能'changeCurrentBook'里面记录的语句,检查是否即使它正在执行或不在onclick事件中。 – RaghavGarg
@RaghavGarg好的电话,它甚至没有开火。 –
所以,在'Publication'组件中添加'onClick'时,你只是传递另一个道具;你必须在你的'Publication'组件中使用(绑定)它到一些'element'来实际激发它。 – RaghavGarg