javascript - Redux 道具记录不正确的数据
问题描述
现在我正在尝试 console.log this.streamCreatorUid,但我遇到了一个特殊的问题。在我的 redux 调试器中,我可以清楚地在正确的位置看到我的数据。
这是我的流创建者的 redux 数据,直接来自我的调试器。
streams -
[0] -
{category(pin): "Oldschool Runescape"
displayName(pin): "admin"
streamId(pin): "98ebc719-c7d5-4558-b99d-2d9f8306ec64"
title(pin): "accounttest"
uid(pin): "wsFc7pIMq5dMtw9hPU86DzUTdLO2"
}
我正在尝试从我的 mapstatetoprops 控制台记录 this.streamCreatorUid,但它正在返回当前Uid
用户u9TcrICehNMlAmqyDHQY77L9CXq1
。我很困惑为什么会这样,考虑到这不是我的调试器中显示的数据。
这是针对个人项目的。过去我可以毫无问题地访问redux
这样的道具,现在我不太确定为什么会发生这种情况。
import React from 'react';
import { database } from '../../../firebaseconfig.js';
import { connect } from 'react-redux';
class StreamFollow extends React.Component {
constructor(props) {
super(props);
this.uid = this.props.uid;
this.displayName = this.props.displayName;
this.streamCreatorUid = this.props.streamCreatorUid;
this.streamCreatorDisplayName = this.props.streamCreatorDisplayName;
}
componentShouldUpdate(prevProps) {
if (this.props.uid !== prevProps.uid) {
this.uid = this.props.uid
}
if (this.props.streamCreatorUid !== prevProps.streamCreatorUid) {
this.streamCreatorUid = this.props.streamCreatorUid;
}
}
//creates a follower object under the stream creators uid
createFollower = (e) => {
const followerRef = database.ref(`User_Follow_Info/${e}/Follower`)
const followerInfoObject = {
uid: this.uid,
displayName: this.displayName
}
followerRef.push(followerInfoObject);
}
//creates a following object under the users uid
//Add in checks to see if following object already exists. We can't follow someone multiple times
createFollowing = (user) => {
const followingRef = database.ref(`User_Follow_Info/${user}/Following`);
const followingInfoObject = {
uid: this.streamCreatorUid,
displayName: this.streamCreatorDisplayName
}
console.log(this.streamCreatorDisplayName)
//Check to see if follow already exists.
/*followingRef.once('value', function (snapshot) {
if (snapshot.hasChild(DATA HERE)) {
alert('exists');
}
}); */
var isSignedIn = this.isSignedIn;
followingRef.orderByChild('uid').equalTo(this.uid).once('value').then(snapshot => {
console.log(snapshot.val());
console.log(this.streamCreatorUid);
if (isSignedIn) {
console.log(snapshot.val())
return
} else {
followingRef.push(followingInfoObject);
}
})
}
onSubmit = () => {
if (this.props.isSignedIn === true) {
this.createFollowing(this.uid);
this.createFollower(this.streamCreatorUid);
} else {
//add in a sign in modal if user is not logged in
console.log('please sign in')
}
}
render() {
return (
<div>
<button onClick={this.onSubmit}>Follow</button>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
isSignedIn: state.auth.isSignedIn,
displayName: state.auth && state.auth.userInfo ? state.auth.userInfo.displayName : null,
uid: state.auth && state.auth.userInfo ? state.auth.userInfo.uid : null,
streamCreatorUid: state.streams && state.streams[0] ? state.streams[0].uid : null,
streamCreatorDisplayName: state.streams && state.streams[0] ? state.streams[0].displayName : null,
}
}
export default connect(mapStateToProps)(StreamFollow);
解决方案
推荐阅读
- moodle - 在主页moodle上列出课程类别
- sql - 两个表的不同计数 - 使用外连接?
- wordpress - Wordpress echo site_url() - 为什么使用 echo?
- c# - Asp.net Core 2 验证所有依赖项都已注册
- facebook-graph-api - 检查用户是否存在于 Facebook
- docker - .dockerignore 不用于 docker-compose 构建?
- concurrency - Spring data JPA - 实体的并发访问
- php - 带有连接表的 Doctrine 查询生成器 ManyToMany
- scala - 在scala中使用嵌套foreach进行迭代是否有效?
- android - 更新到新的 Android Studio 3.1.2