我有3個反應部件 - 容器(SearchBarContainer)和2x表象成分(PostList & PostItem PostList的(孩子))陣營 - 單擊處理程序子組件上地圖不工作
我試圖附加點擊處理程序後的項,泡到SearchBarContainer,這裏是我的代碼:
class SearchBarContainer extends Component {
selectPost(post) {
console.log('cliked');
//this.props.dispatch(selectPost(post));
}
render() {
return (
<div>
<div className="center-xs">
<p>To get started, type in some keywords below</p>
</div>
<SearchBar onTermChange={this.props.actions.loadPosts} onNull={this.props.actions.nullPosts}/>
<PostList posts={this.props.posts} isFetching={this.props.isFetching} onClick={this.selectPost}/>
</div>
);
}
}
這裏是我的postList:
const PostList = (props) => {
const postItems = props.posts.map((singlePost) => {
return <PostItem key={singlePost.id} post={singlePost} onClick={() => props.onClick}/>
});
if(!props.isFetching) {
return (
<div className="mt2 row list-unstyled" > {postItems}</div>
);
} else {
return (
<Loader />
);
}
};
export default PostList;
但是,這個點擊處理程序不工作。我注意到如果我將click處理程序放在包裝postItems數組的div上,那麼它確實起作用,我認爲問題在於click處理程序附加到包含在div內的映射項目中?
這裏是PostItem功能:
const PostItem = (singlePost) => {
return (
<div className="col-lg-4 col-md-12 mb2 animated fadeIn">
<div className="card rounded">
<div className="card-header p1">
<h4 className="card-title mt0 h5">{singlePost.post.title}</h4>
</div>
<img className="card-img-top img-fluid" src={singlePost.post.thumbnail} alt={singlePost.post.title} />
<div className="card-block p1">
{/*<p className="card-text">{singlePost.post.description}</p>*/}
<Link to={`/product/${singlePost.post.slug}`} className="btn btn--blue flt--left w100">View More</Link>
</div>
</div>
</div>
)
};
export default PostItem;
我想,也許我需要單擊處理事件是這樣的: 點擊(PostItem) - >通過道具通(PostList) - >通過道具通( SearchBarContainer)
可能重複的[React - 爲什麼事件沒有被解僱](https://stackoverflow.com/questions/44070162/react-why-event-is-not-being-fired) –
除了它是一個重複,您需要進行@Mayank在回答中建議的更改 –