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