reactjs - 为用户评分添加积分反应
问题描述
每次按下按钮时,我都会尝试向用户配置文件添加点。积分应该累积。
进入模态组件时,我可以将点添加到配置文件中(该组件具有添加点的按钮,并且只能按下一次)。但是,当我重新加载模式时,现在使用禁用的按钮,点仍在更新但将变为空。
另一个问题是积分没有被累积,而是每次都被替换。
“点”是问题。其他一切正常。
const EventLog = ({ current, logPointsNow, logPoints }: IEventLog) => {
const [dateLogged, setDateLogged] = useState("");
const [points, setPoints] = useState("");
useEffect(() => {
if (current) {
setDateLogged(current.dateLogged);
}
}, [current]);
const handleClick = () => {
setDateLogged(Date());
setPoints(points + String(50));
};
useEffect(() => {
const updatedDate = {
dateLogged,
};
logPointsNow(updatedDate);
}, [dateLogged]);
useEffect(() => {
const meetingPoints = {
points,
};
logPoints(meetingPoints);
}, [points]);
return (
<IonContent>
<UserPoints />
<IonButton
type="submit"
expand="block"
onClick={handleClick}
disabled={arrivalTime ? true : false}
>
Log
</IonButton>
<IonItem>
<IonLabel>Points earned: {points}</IonLabel>
</IonItem>
</IonContent>
);
};
const mapStateToProps = (state: IEventLogReduxProps) => ({
current: state.event.current,
});
export default connect(mapStateToProps, { logPointsNow, logPoints })(EventLog);
这是获取用户信息的组件:
const UserPoints = ({ auth }: IUserP) => {
return (
<IonItem>
{auth!.user.points}
{auth!.user.name}
</IonItem>
);
};
const mapStateToProps = (state: IAuthReduxProps) => ({
auth: state.auth,
});
export default connect(mapStateToProps, null)(UserPoints);
解决方案
我修复了第一个问题:
当进入模态组件时,我可以将点添加到配置文件中(该组件具有添加点的按钮,并且只能按下一次)。但是,当我重新加载模式时,现在使用禁用的按钮,点仍在更新但将变为空。
通过执行以下操作:
useEffect(()=>{
if(dateLogged) {
const meetingPoints = {
points
};
logPoints(meetingPoints)
}
}, [points])
推荐阅读
- laravel-5 - 方法 App\Http\Requests\EndTripRequest::rules() 在第 142 行的文件 vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php 中不存在
- node.js - 引用关系上的嵌套包含在续集中不起作用
- linux - 如何在 Linux 命令行中从 JPEG 获取日期和时间?
- javascript - 如何在 VUE 中使用这个外部非 VUE 库
- python - Python - 如何在忽略内部数组的情况下将字符串拆分为数组?
- vue.js - Vue:使用 v-for 创建列布局
- html - 如何让 Angular 重新加载相同的页面但使用不同的参数?
- excel - 基于字符串的自定义排序
- python - 来自 Python 嵌套字典的 Pandas Dataframe
- python - 使用 `socket` 模块的 Python 代理服务器的奇怪响应主体