我正在使用一個網站,我的岳父讓我建立作爲學習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