[英]Why are my notes not displayed/updated, even though reactjs received the data?
I'm trying to load my notes from my Firebase database. 我正在尝试从Firebase数据库加载笔记。 Adding them goes fine and retrieving my notes is also fine (I can confirm that it's working via console and checking my firebase database by going to console.firebase.google.com).
添加它们很好,检索我的笔记也很好(我可以通过console并通过console.firebase.google.com来检查我的Firebase数据库,以确认它是否正常工作)。
I am using React-Redux for the first time on my project. 我在我的项目中第一次使用React-Redux。
I already tried debugging and checking if the correct data has been shown by console logging the variables. 我已经尝试调试并检查控制台记录变量是否显示了正确的数据。 Everything goes fine, however nothing is displayed or updated.
一切正常,但是什么也没有显示或更新。
App.js:
App.js:
componentWillMount(){
const previousNotes = Array(0);
this.removeAuthListener = app.auth().onAuthStateChanged((user) => {
app.database().ref().child('notities').on('child_added', snap => {
if(snap.val().username === this.props.email){
console.log('DB_ADDED');
previousNotes.push({
id: snap.key,
noteContent: snap.val().noteContent,
modifiedDate: snap.val().modifiedDate
});
console.log("previousNotes", previousNotes);
store.dispatch({type: "DB_NOTE_ADDED", payload: { notes: previousNotes }});
}
});
NotesBody.js:
NotesBody.js:
import React from 'react';
import { connect } from "react-redux";
class NotesBody extends React.Component {
render(){
return(
<ul>
{
this.props.notes.map((note) => {
return <li key={note.id}>{note.noteContent}</li>
})
}
</ul>
);
}
}
function mapStateToProps(state){
console.log("[STATE NOTESBODY]", state);
return {
notes: state.notes
}
}
export default connect(mapStateToProps)(NotesBody);
My reducer:
我的减速器:
import { createStore } from 'redux';
import { app } from '../config/config';
const initialState = {
database: app.database().ref().child('notities'),
notes: []
}
const reducer = (state = initialState, action) => {
console.log("Reducer running", action.type);
switch(action.type){
default:
break;
case "DB_NOTE_ADDED":
console.log("DB_NOTE_ADDED", action.payload.notes);
state = {
...state,
notes: action.payload.notes
}
break;
}
return state;
}
const store = createStore(reducer);
store.subscribe(() => {
console.log("Store updated", store.getState());
});
export default store;
When my code has received the notes, it calls the dispatch function with the notes array in it: 当我的代码收到注释时,它将调用带有注释数组的调度函数:
var previousNotes = [
{ id: "1", noteContent: "Hoi" }
];
store.dispatch({type: "DB_NOTE_ADDED", payload: { notes: previousNotes }})
Actual result: 实际结果:
Expected result: 预期结果:
EDIT: Added my action code. 编辑:添加了我的动作代码。 Now I only have 1 note loaded, if I implement the first solution below.
现在,如果我实现以下第一个解决方案,则仅加载1个便笺。
I suggest using some sort of loading state on that NotesBody.js
component, just until you are sure your data is successfully stored. 我建议在该
NotesBody.js
组件上使用某种加载状态,直到您确定数据已成功存储为止。 That being said add loading: true
to your redux state, and only set it to false
when you successfully get notes and update the state. 话虽这么说,将
loading: true
为redux状态,并且仅在成功获取注释并更新状态时将其设置为false
。
class NotesBody extends React.Component {
render() {
let notes = <p>Loading ...</p>;
if (!this.props.loading) {
notes = this.props.notes.map(note => (
<li key={note.id}>{note.noteContent}</li>
));
}
return <ul>{notes}</ul>;
}
}
function mapStateToProps(state) {
console.log("[STATE NOTESBODY]", state);
return {
notes: state.notes,
loading: state.loading
};
}
And your switch should look like this: 您的开关应如下所示:
switch(action.type){
default:
break;
case "DB_NOTE_ADDED":
console.log("DB_NOTE_ADDED", action.payload.notes);
state = {
...state,
notes: action.payload.notes,
loading: false
}
break;
}
PS. PS。 Don't forget to switch loading to true when you decide to request new notes from your API.
当您决定从API请求新笔记时,请不要忘记将load切换为true。
Well shoot me in the face.. I managed to get it working in this way: 好吧,拍我的脸..我设法使它以这种方式工作:
function mapStateToProps(state){
return {
notes: state.notes,
notesLength: state.notes.length
}
}
If I add something else than the length of the notes, the problem rises again.. What the.. 如果我添加了笔记长度以外的其他内容,问题将再次出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.