简体   繁体   English

为什么不能从 redux 读取我的属性属性?

[英]Why can’t react read my properties property from redux?

Why can't react read my properties property from redux为什么无法从 redux 读取我的属性

I made a ReactJS and redux application to real and to handle redux actions but when I run the code react says that the properties aren't defined.我制作了一个 ReactJS 和 redux 应用程序来处理 redux 操作,但是当我运行代码时,react 表示未定义属性。 I tried tweaking both the initState and index.js file.我尝试调整 initState 和 index.js 文件。 If you want the full file this is the link https://codesandbox.io/s/landing-page-forked-rkq6f?file=/src/reducers/initState.js and type this in the mini browser rkq6f.csb.app/search/1如果你想要完整的文件,这是链接https://codesandbox.io/s/landing-page-forked-rkq6f?file=/src/reducers/initState.js并在迷你浏览器中输入 rkq6f.csb.app /搜索/1

index.js索引.js

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import { ThemeProvider } from "styled-components";
import { theme } from "./Components/UI";
import App from "./App";
import { Provider } from "react-redux";
import { StoreReducer } from "./reducers/reducer";
// reducers
import thunk from "redux-thunk"
import logger from "redux-logger"
import { createStore,applyMiddleware } from "redux"

const store = createStore(StoreReducer,applyMiddleware(thunk,logger))
console.log(store)

ReactDOM.render(
  <Provider store={store}>
    <BrowserRouter>
        <App />
    </BrowserRouter>
  </Provider>,
  document.getElementById("root")
);

initState.js initState.js

const initState = {
  properties: [],
  loading: false,
  error: false
};
export default initState;

reducer.js减速器.js

import initState from "./initState";

export const StoreReducer = (state = initState, action) => {
...
   }
};

The Problem is that you reducer is not returning default state ,问题是你的减速器没有返回默认状态,

 import initState from "./initState"; export const StoreReducer = (state = initState, action) => { switch (action.type) { case "LOADING_PROPERTIES": // Update total price of cart items // Subtract item base price + item quantity return { ...state, loading: true }; case "FETCH_PROPERTIES": // Update total price of cart items // Subtract item base price + item quantity return { ...state, properties: action.payload, loading: false }; case "FETCH_PROPERTIES_ERROR": // Update total price of cart items // Subtract item base price + item quantity return { ...state, loading: false, error: true }; /* this ONe */ default: null; } };

you are not returning anything .你没有返回任何东西。

instead the reducer by default need to return the initial State而默认情况下,reducer 需要返回初始状态

here is Working reducer :这是工作减速器:

 import initState from "./initState"; export const StoreReducer = (state = initState, action) => { switch (action.type) { case "LOADING_PROPERTIES": // Update total price of cart items // Subtract item base price + item quantity return { ...state, loading: true }; case "FETCH_PROPERTIES": // Update total price of cart items // Subtract item base price + item quantity return { ...state, properties: action.payload, loading: false }; case "FETCH_PROPERTIES_ERROR": // Update total price of cart items // Subtract item base price + item quantity return { ...state, loading: false, error: true }; default: return initState; } };

暂无
暂无

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

相关问题 为什么我不能在 React/Redux 中索引我的对象? 类型错误:无法读取未定义的属性“1” - Why Can't I index my object in React/Redux? TypeError: Cannot read property '1' of undefined React 无法读取从 Redux state 获得的值 - React can't read value gotten from Redux state 为什么我的api json数据无法从循环读取我的属性并将html数据附加到dom? - Why my api json data can't read my properties from the loop and append the html data to the dom? React / Redux:为什么我在this.props中的属性未定义? - React/Redux: Why are my my properties in this.props undefined? 为什么我的redux将映射属性存储到具有该值的单个属性的对象? - Why is my redux store mapping properties to an object with a single property with the value? React-redux:当它在功能组件中工作时,为什么我不能访问类组件中的状态属性? - React-redux: Why can't I access state properties in a class component, when it works in a functional component? 使用 Redux 执行操作后“无法读取 &#39;undefined&#39; 的属性” - "Can't read properties of 'undefined'" after excecuting an action with Redux 从Redux Connect获取属性的React元素中的React Redux流类型“找不到属性” - React Redux Flowtype “property not found” in React element that gets properties from redux connect 为什么我的Redux商店没有获得Firebase电子邮件属性? 应对 - Why my redux store is not getting firebase email property? React 反应来自 Redux 的工作。 TypeError:无法读取未定义的属性“类型” - React work from Redux . TypeError: Cannot read property 'type' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM