我目前正在構(gòu)建一個(gè)由 Redux 和 Firebase 支持的小型 React 應(yīng)用程序。我添加的功能之一是切換/保存用戶個(gè)人資料的喜歡。如果某些東西已經(jīng)被喜歡,只需單擊一下即可取消喜歡,再單擊一次即可重新喜歡。但是,在新加載時(shí),第一次喜歡某事物需要雙擊,然后每個(gè)后續(xù)事件都需要單擊一次來喜歡/不喜歡。起初我以為這e.preventDefault()是搞砸了,但我刪除了它,問題仍然存在。我把它歸咎于它Link,但我把它換成了一個(gè)普通的div,仍然有同樣的問題。我怎樣才能擺脫雙擊來第一次喜歡某事?子組件import React, { Component } from "react";import { connect } from "react-redux";import { saveFavorite } from "../redux/actions";class VenueSummary extends Component { state = { loved: false }; toggleHeart = e => { e.preventDefault(); this.props.saveFavorite(this.props.venue, this.state.loved); this.setState({ loved: !this.state.loved }); }; render() { const { venue, loved, name } = this.props; return ( <div className="card-title"> {name} <i className={loved ? "fas fa-heart right" : "far fa-heart right"} onClick={this.toggleHeart} /> </div> ); }}const mapDispatchToProps = dispatch => { return { saveFavorite: (venue, loved) => dispatch(saveFavorite(venue, loved)) };};export default connect( null, mapDispatchToProps)(VenueSummary);
有時(shí)需要雙擊,但并非總是如此
qq_笑_17
2021-08-26 15:11:03