简体   繁体   English

无法从 redux 存储中获取数据

[英]Cannot get data out from redux store

I can see all the data I want in the redux state from the website extensions, however, when I want to pull it out to use it in my component it said this.props.rules is undefined:我可以从网站扩展中在 redux state 中看到我想要的所有数据,但是,当我想将其拉出以在我的组件中使用它时,它说 this.props.rules 未定义:

Here is the create-store.js:这是 create-store.js:

import {createStore, applyMiddleware} from 'redux';
import mainReducer from './reducers/main-reducer';
import {composeWithDevTools} from 'redux-devtools-extension';
import thunk from 'redux-thunk';

export default () => {
  return createStore(mainReducer, composeWithDevTools(
    applyMiddleware(thunk)
  ))
}

Here is the mainReducer.js这是 mainReducer.js

import {combineReducers} from 'redux';

import rules from './rules-reducer';

export default combineReducers({
  rules
});

Here is the rules reducer:这是规则缩减器:

const initialState = {
  allRules: [],
  singleRule: {}
}

export default (state = {}, {type, payload}) => {
  switch(type){
    case 'GET_RULES':
      console.log('got in get_rules reducer');
      return {
        ...state,
        allRules: payload
      }
    default:
      return state;
      }

};

Here is the getRulesAction:这是 getRulesAction:

export const getRulesAction = () => dispatch => {
  console.log('got into getrulesaction');
  const sheets = Object.entries(document.styleSheets);
  let allRules = [];
  for(let i = 0; i < sheets.length; i++){
    const sheet = sheets[i][1];
    const rules = Object.entries(sheet.cssRules);
    for(let j = 0; j < rules.length; j++){
      // console.log(rules[j][1].cssText);
      allRules.push(rules[j][1].cssText);
    }  
  }
  dispatch({
    type: 'GET_RULES',
    payload: allRules
  })
  
};

Here is the component I want to show my data:这是我要显示数据的组件:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {getRulesAction} from '../actions/ruleActions';

class Rules extends Component {
  componentWillMount() {
    this.props.getRulesAction();
    console.log('props from will mount', this.props);
  }

  render(){
    console.log(this.props.rules);
    const fetchedRules = this.props.rules.map((rule,i) => {
        return <li key={i}>{rule}</li>
    }
    )

    return(
      <div>
        <h2>Component to get rules from the redux store</h2>
        {fetchedRules}
      </div>
    );
  }

}

Rules.propTypes = {
  getRulesAction: PropTypes.func.isRequired,
  rules: PropTypes.array.isRequired,
}

const mapStateToProps = state => ({
  rules: state.rules.allRules
})

export default connect(mapStateToProps, {getRulesAction})(Rules);

在此处输入图像描述

The error is most probably due to the following.该错误很可能是由于以下原因。 When you declare a state, you need to initialize the state props.当你声明一个 state 时,你需要初始化 state 属性。 You do this by passing a default state to the reducer when it runs for the first time.您可以在第一次运行时将默认 state 传递给减速器。 However, your default state is set to en empty object:但是,您的默认 state 设置为空 object:

export default (state = {}, {type, payload})

Therefore, when the component renders for the first time, this.props.rules is undefined.因此,当组件第一次渲染时, this.props.rules是未定义的。 To remedy this, you'd want to initialize the state with initialState , which would ensure that this.props.rules is an array instead.为了解决这个问题,您需要使用initialState初始化 state,这将确保this.props.rules是一个数组。

export default (state = initilState, {type, payload})

Later, when the action is fired, the result would be pulled from the updated state.稍后,当触发该操作时,将从更新的 state 中提取结果。

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

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