简体   繁体   English

React redux 不会更新 class 组件的 state

[英]React redux won't update class component's state

I have a component which is supposed to update if a state from another component has been changed using redux, but it's not.如果使用 redux 更改了来自另一个组件的 state,我有一个组件应该更新,但事实并非如此。

Inside mapStateToProps the correct value is being returned on redux action.mapStateToProps中,redux 操作将返回正确的值。

import React, {Component} from "react";
import './DashboardContent.scss';
import * as entries from '../../../assets/demo.json';

import CategoryHeader from "./CategoryHeader/CategoryHeader";
import NoContent from "../../../shared/NoContent/NoContent";
import UnsortedList from "./UnsortedList/UnsortedList";
import {connect} from "react-redux";

class DashboardContent extends Component {

    state = {
        activeCategory: this.props.activeCategory
    };

    componentDidUpdate(prevProps, prevState, snapshot) {
        console.log(this.props.activeCategory)
    }

    render() {
        return (
            <div>
               ...
            </div>
        )
    }

}

const mapStateToProps = state => {
    console.log(state);
    return {
        activeCategory: state.category
    }
};

export default connect(mapStateToProps)(DashboardContent);



Dispatch inside of another component - when this is being executed, the state activeCategory of first component shall become some value :在另一个组件内部调度 - 执行此操作时,第一个组件的 state activeCategory应变为some value

dispatch(changeCategory('some value'))



Actions.js Actions.js

// Action types
const CHANGE_CATEGORY = 'CHANGE_CATEGORY'


// Action creators
export const changeCategory = (category) => {
    return {
        type: CHANGE_CATEGORY,
        category
    }
}



Reducer.js减速器.js

const initialState = {
  activeCategory: 'all'
};

export const reducer = (state = initialState, action) => {
    console.log('reducer', state, action);
    if (action.type === 'CHANGE_CATEGORY') {
        return action.category
    }
    return state;
};

Your reducer should return the updated state instead it returns action.category atm.您的减速器应该返回更新后的 state,而不是返回 action.category atm。

export const reducer = (state = initialState, action) => {
  console.log('reducer', state, action);
  if (action.type === 'CHANGE_CATEGORY') {
   return {...state,category:action.category}; 
  }
  return state;
};

What happens when you add this to the main class?(class DashboardContent extends Component) I believe you need to do that when you try to access the this.props.activeCategory.当您将它添加到主 class 时会发生什么?(类 DashboardContent 扩展组件)我相信当您尝试访问 this.props.activeCategory 时需要这样做。 Try to add this snippet.尝试添加此代码段。

 constructor(props) { 
 super(props); 
 this.state = this.props.activeCategory;
 } 

You may refer to this answer: What's the difference between "super()" and "super(props)" in React when using es6 classes?你可以参考这个答案: 使用 es6 类时 React 中的“super()”和“super(props)”有什么区别?

when you are connecting a component to redux, the second argument is the dispatchToProps, is the only way you can dispatch an action inside your component当您将组件连接到 redux 时,第二个参数是 dispatchToProps,这是您可以在组件内调度操作的唯一方法

export default connect(mapStateToProps, {action1, action2,})(DashboardContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM