简体   繁体   English

为什么即使reactjs收到了数据,我的笔记为什么也没有显示/更新?

[英]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.

相关问题 为什么所有数据都显示在 console.log() 中,但浏览器上却没有显示所有数据? - Why won't all the data get displayed on the browser even though all data is being displayed in console.log()? (ReactJS)即使数据库已更新,项目列表也不会在页面上更新 - (ReactJS) List of items doesn't get updated on page, even though database gets updated Mongodb Atlas 集合中没有收到数据,即使与应用程序建立了连接 - No data received in Mongodb Atlas collection, even though connection is established with app *ngFor 即使数据更新也不显示数据 - *ngFor does not display data even though the data is updated 即使数组有元素,在 ReactJS 中映射数组时,浏览器中也不会显示任何内容 - Nothing is displayed in Browser while mapping through an array in ReactJS even though the array has elements 为什么在 ReactJS 中更新 state 时,我的数据在旧数据和新数据之间闪烁? - Why my data is flickering between previous and new when state is updated in ReactJS? 为什么即使控制台记录了我的Ajax JSON数据也不能正确返回? - Why does my JSON data with Ajax not return correctly even though the console logs it? 即使定义了字段,为什么我的谷歌位置附近搜索返回气氛和联系数据? - Why is my google places nearbysearch returning atmosphere and contact data even though fields are defined? 为什么即使它适用于 Postman,它也不能成功发布我的数据? - Why won't it POST my data successfully even though it works on Postman? 为什么我的锚标记onclick()即使触发警报也不会将我的数据属性发送到购物车? - Why isn't my anchor tag onclick() sending my data properties to cart even though it's firing my alert?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM