- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
单击一个赞后,它会增加两个单独组件的赞数。 是什么导致两个喜欢的数字都增加了,我如何将其编码为点击一个喜欢的数字只会增加一个?
我还在下图中包含了控制台,其中控制台记录了我的 reducer 中的逻辑。您可以在图片下方找到 reducer 的代码。
reducer 代码
import { GET_GOALS, GOAL_ERROR, UPDATE_LIKES } from "../actions/types";
const initialState = {
goals: [],
goal: null,
loading: true,
error: {}
};
export default function(state = initialState, action) {
const { type, payload } = action;
switch (type) {
case GET_GOALS:
return {
...state,
goals: payload,
loading: false
};
case GOAL_ERROR:
return {
...state,
error: payload,
loading: false
};
case UPDATE_LIKES:
return {
...state,
goals: state.goals.map(goal =>
console.log("goal id", goal._id) === console.log("payload id", payload.goalId) ? { ...goal, likes: payload.likes } : goal
),
loading: false
};
default:
return state;
}
}
import axios from "axios";
import { GET_GOALS, GOAL_ERROR, UPDATE_LIKES } from "./types";
// Get goals
export const getGoals = () => async dispatch => {
try {
const res = await axios.get("/api/goal/goalfeed");
dispatch({
type: GET_GOALS,
payload: res.data
});
} catch (error) {
dispatch({
type: GOAL_ERROR,
payload: { msg: error.response }
});
}
};
// Add like
export const addLike = goalId => async dispatch => {
try {
const res = await axios.put(`/api/goal/like/${goalId}`);
dispatch({
type: UPDATE_LIKES,
payload: { goalId, likes: res.data }
});
} catch (error) {
dispatch({
type: GOAL_ERROR,
payload: { msg: error.response }
});
}
};
// Remove like
export const removeLike = goalId => async dispatch => {
try {
const res = await axios.put(`/api/goal/unlike/${goalId}`);
dispatch({
type: UPDATE_LIKES,
payload: { goalId, likes: res.data }
});
} catch (error) {
dispatch({
type: GOAL_ERROR,
payload: { msg: error.response }
});
}
};
import React, { useEffect } from "react";
import Moment from "react-moment";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
import { connect } from "react-redux";
import { addLike, removeLike } from "../../actions/goal";
import { getGoals } from "../../actions/goal";
import Spinner from "../layout/Spinner";
import Navbar from "../dashboard/Navbar";
import ThumbUpAltIcon from "@material-ui/icons/ThumbUpAlt";
import ThumbDownAltIcon from "@material-ui/icons/ThumbDownAlt";
import ChatIcon from "@material-ui/icons/Chat";
import DeleteIcon from "@material-ui/icons/Delete";
import DoneIcon from "@material-ui/icons/Done";
import {
Typography,
Container,
CssBaseline,
makeStyles,
Grid,
Avatar,
Paper,
Button
} from "@material-ui/core";
const useStyles = makeStyles(theme => ({
paper: {
height: "auto",
marginBottom: theme.spacing(3)
},
actionButtons: {
marginTop: "3vh"
},
profileHeader: {
textAlign: "center",
marginBottom: 20
},
avatar: {
width: theme.spacing(7),
height: theme.spacing(7)
}
}));
const Goals = ({
getGoals,
auth,
addLike,
removeLike,
goal: { goals, user, loading }
}) => {
useEffect(() => {
getGoals();
}, [getGoals]);
const classes = useStyles();
return loading ? (
<>
<Navbar />
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<Spinner />
</div>
</Container>
</>
) : (
<>
<CssBaseline />
<Navbar />
<main>
<Container>
<Typography variant="h2" className={classes.profileHeader}>
Goals
</Typography>
{/* parent grid */}
<Grid container spacing={4}>
{goals.map(singleGoal => (
<Grid
className={classes.paper}
key={singleGoal._id}
spacing={1}
container
item
direction="row"
alignItems="center"
component={Paper}
>
<Grid
item
container
direction="column"
justify="center"
alignItems="center"
xs={3}
>
<Avatar className={classes.avatar} src={singleGoal.avatar} />
<Typography variant="caption">
{singleGoal.first_name} {singleGoal.last_name}
</Typography>
<Typography variant="caption" className={classes.postedOn}>
Posted on{" "}
<Moment format="MM/DD/YYYY">{singleGoal.date}</Moment>
</Typography>
</Grid>
<Grid container item direction="column" xs={9}>
<Typography variant="body1">{singleGoal.text}</Typography>
<Grid item className={classes.actionButtons}>
<Button size="small" onClick={e => addLike(singleGoal._id)}>
<ThumbUpAltIcon />
</Button>
<Typography variant="caption">
{singleGoal.likes.length}
</Typography>
<Button
size="small"
onClick={e => removeLike(singleGoal._id)}
>
<ThumbDownAltIcon />
</Button>
<Button href={`/goal/${singleGoal._id}`} size="small">
<ChatIcon />
</Button>
{!auth.loading && singleGoal.user === auth.user._id && (
<Button size="small">
<DoneIcon />
</Button>
)}
{!auth.loading && singleGoal.user === auth.user._id && (
<Button size="small">
<DeleteIcon />
</Button>
)}
</Grid>
</Grid>
</Grid>
))}
</Grid>
</Container>
</main>
</>
);
};
Goals.propTypes = {
getGoals: PropTypes.func.isRequired,
goal: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
goal: state.goal,
auth: state.auth
});
export default connect(mapStateToProps, { getGoals, addLike, removeLike })(
Goals
);
最佳答案
您的条件测试存在缺陷。
state.goals.map(goal =>
console.log("goal id", goal._id) === console.log("payload id", payload.goalId) // What is this? it will always evaluate to true
? { ...goal, likes: payload.likes }
: goal
)
console.log('EQUAL?', console.log() === console.log()); // true
console.log('EQUAL?', console.log(3) === console.log(3)); // true
console.log('EQUAL?', console.log(3) === console.log('three')); // true
console.log('EQUAL?', console.log('apple') === console.log({})); // true
console.log('EQUAL?', console.log(42) === console.log(-42)); // true
console.log
是一个无效的返回,即
undefined
,所以你在比较
undefined === undefined
,这总是正确的。
console.log(undefined === undefined); // true
state.goals.map(
goal => goal._id === payload.goalId
? { ...goal, likes: payload.likes }
: goal
)
关于javascript - 单击喜欢的图标会增加其他组件的喜欢计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59763445/
假设您有下表,名为 Likes: A|B --- a|b a|f a|e a|i b|a b|i c|d e|p 在此表中,A 中的值表示“喜欢”B 中的人。因此,a 喜欢 b,a 喜欢 f,a 喜欢
请访问此处观看直播https://ibnul.neocities.org/_temporary/au2pr4/like-dis-icon-active-effect.html 这里我有多个评论部分,每
我想要一个喜欢/不喜欢的超链接来在我的页面上显示不同的内容:当点击“喜欢”时,显示“好”;单击“不喜欢”时,显示“不好”。我的代码是这样的: function Homepage(){ t
我们已经建立了一个 Facebook Page Tab application用Like Gate .我们之前已经做过无数次了,在这方面,这个应用程序没有任何异常。 在我们的 ASP.Net 4.0
我正在尝试在我的页面上实现一个喜欢/不喜欢按钮。我设法让按钮工作(点击时它会变成喜欢,反之亦然),它还会在数据库表上创建或删除类似的东西。现在的问题是喜欢的计数器。它仅在我第一次单击按钮时有效,即如果
我想在我的 Android 应用程序中集成一个赞按钮。我用了代码 likeWebView = (WebView) findViewById( R.id.webView1 ); likeWebView
我有一个漫画网站,我希望用户可以为每部漫画和每件艺术品一次投票。 我的代码似乎有两个问题: 1) 我只希望一个用户每张图片投票一次...所以我想捕获他们的信息并将其存储在数据库中。我有一个 ON DU
我正在开发 web 应用程序,我必须像 facebook 那样实现“喜欢”系统。应用程序将包含客户可以“喜欢”的几类产品。所以我开始创建数据库,但遇到了一个障碍。据我了解,有两种方法可以做到这一点:
我需要一个 mysql 查询方面的帮助。我无法找到特定查询的解决方案。我什至不确定,如果 LIKE 运算符是我正在寻找的。 我想要实现的是,找到所有包含 url 的行,如果该 url 不是 foo.c
如何为 Like 编写动态 LINQ 方法条款。 供引用,有Dynamic LINQ OrderBy on IEnumerable / IQueryable .我正在寻找一个类似的动态 Like条款。
This question already has answers here: Cumulative number of unique values in a column up to current
我需要任何用户能够每 24 小时点赞一次 我为此写了一个函数 const LIKE_HEART = 'like_heart'; const LIKE_FINGER = 'like_finger'; p
让我在这里解释一下整个事情,以便您可以清楚地了解情况: 我在 Facebook 上有一个页面,这些见解(页面上的和来自图形 API 的)为我提供了很多有值(value)的信息,但我需要更深入。我正在考
给定这个脚本: DECLARE @token NVARCHAR(max) SET @token = 'mytexttosearchfor' SELECT * FROM myTable WHERE my
我怎样才能做一个Like-query,要搜索多个值? $searchWords = explode(' ', Input::get('search')); 然后我得到一组用于搜索的单词。 我怎样才能通
我正在尝试在页面上制作一个“赞”按钮,但似乎无法使其正常工作。基本上有三个函数使用ajax将数据发送到更新数据库的php页面。我已经检查了数据库并且所有三个更新都正确。如果用户最初不喜欢并单击,它会正
我有两张 table 在“用户”表中,每个用户都有一些技能。它们位于单个列上并连接在一起。在表“技能”上,每个技能都有一个关联的标签。 碰巧有些用户的技能不再在表“技能”中引用。 我想做的选择应该列出
所以我正在尝试设计一个数据库来跟踪观看次数、喜欢次数和下载次数。现在要跟踪的条目数量预计为 1m 或更多,所以通常我会每天跟踪每个条目,但对于 1m,我担心性能甚至可能是硬盘大小。 客户希望能够显示上
我像在 Firebase 事件数据库中一样实现了该系统。点赞计数器位于 EventModel -> likesInfo -> likesNumber 处。问题在于数据发散,例如,如果两个用户快速(0.
假设两个用户正在使用一个应用程序,并已授予该应用程序适当的权限来检索他们的点赞。是否可以使用 FQL 或图形 api 来查找他们的共同点?类似于如何使用图形 API 在两个用户之间找到共同的 frie
我是一名优秀的程序员,十分优秀!