私の義理の義理が私にReactを学ぶ言い訳をするよう求めているウェブサイトを使用しています。私は自然なJavaScript開発者(バックエンドでは幸せ)ではなく、おそらく私のコードにいくつかの再帰的な要素があることを知っていますが、私はそれを見ることができません。誰かが私が間違っていることを教えてもらえますか?あなたはReact - 呼び出しスタックの最大サイズを超えました
onClick={ this.changeCurrentBook(publication) }
を書く場合
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;
のようなあなたの方法をバインドする必要があり、あなたの
Publication
コンポーネントのonClick
このなどを書きますonclickイベントで実行されているかどうかに関係なく、実行されます。 – RaghavGarg
@RaghavGargいいですよ、それは発砲していません。 –
したがって、 'onClick'を' Publication'コンポーネントに追加するときには、別のpropを渡すだけです。あなたの 'Publication'コンポーネントの中でそれを実際に起動させるために' element'に使う(バインドする)必要があります。 – RaghavGarg