在这段代码中,我使用了REACTION-REDUX和REPACT-RUTER.Reaction-Redux版本是旧的,但我想知道我在这段代码中哪里出错了(在这个版本的Reaction-Redux和Reaction-Router中我指的是). 我try 在main.jsx中得到ingredients,并在OrderSummary组件中使用它,但我得到了如下错误:

  • 无法读取未定义的属性,
  • 找不到州/自治区.

github repository

main.jsx:

import ReactDOM from "react-dom/client";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Provider } from "react-redux";
import { createStore } from "redux";
import reducer from "./store/reducer";

export default function Main(props) {
  const store = createStore(
    reducer /* preloadedState, */,
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  );

  return (
    <Provider store={store}>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Layout />}>
            <Route index element={<App />} />
            <Route
              path="/burger-builder/order-page"
              exact
              element={
                <OrderSummary
                  ingredients={props.ingredients}
                  totalPrice={props.totalPrice}
                />
              }
            />
            <Route path="*" element={<NoPage />} />
          </Route>
        </Routes>
      </BrowserRouter>
    </Provider>
  );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<Main />);

BurgerBuilder.jsx:

import * as actionTypes from "../../store/action";
import { connect } from "react-redux";

function BurgerBuilder(props) {
return (
    <>
      <Burger ingredients={props.ings} />
      <BurgerControls
        addIngredients={props.onIngredientAdded}
        removeIngredients={props.onIngredientRemoved}
        totalprice={props.totalPrice}
        disabled={disableButton}
      />
    </>
  );
}

const mapStateToProps = (state) => {
  return {
    ings: state.ingredients,
    price: state.totalPrice,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    onIngredientAdded: (ingName) =>
      dispatch({ type: actionTypes.ADD_INGREDIENT, ingredientName: ingName }),
    onIngredientRemoved: (ingName) =>
      dispatch({
        type: actionTypes.REMOVE_INGREDIENT,
        ingredientName: ingName,
      }),
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(BurgerBuilder);

part of

reducer.js:

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case actionTypes.ADD_INGREDIENT:
            return {
                ...state,
                ingredients: {
                    ...state.ingredients,
                    [action.ingredienName]: state.ingredients[action.ingredientName] + 1
                },
                totalPrice: state.totalPrice + INGREDIENT_PRICES[action.ingredientName]
            };

推荐答案

Main组件中,您将props.ingredientsprops.totalPrice传递给OrderSummary,但props不是在Main中定义的--当您使用root.render(<Main />);渲染Main时,不会传递任何参数.

要解决这一问题,你可以像连接BurgerBuilder一样,将OrderStatus连接到Reduxstore :


function OrderSummary(props) {
    // access mapped props
    const { ingredients, totalPrice } = props;

    // ...
};

const mapStateToProps = (state) => {
    return {
        ingredients: state.ingredients,
        totalPrice: state.totalPrice,
    };
};

export default connect(mapStateToProps)(OrderSummary);

然后,在Main中,不使用props渲染OrderSummary:

<Route path="/burger-builder/order-page" exact element={<OrderSummary />} />

还有,你的减速机有一个打字错误.你写的是action.ingredienName,而不是ADD_INGREDIENTaction.ingredientName.以下是已修复的Reducer.js:

case actionTypes.ADD_INGREDIENT:
    return {
        ...state,
        ingredients: {
            ...state.ingredients,
            [action.ingredientName]: state.ingredients[action.ingredientName] + 1
        },
        totalPrice: state.totalPrice + INGREDIENT_PRICES[action.ingredientName]
    };

Javascript相关问答推荐

为什么子组件没有在reaction中渲染?

Angular material 表多个标题行映射

nPM审计始终发现0个漏洞

在JS中获取名字和姓氏的首字母

在React中获取数据后,如何避免不必要的组件闪现1秒?

拖放仅通过 Select 上传

Angular中计算信号和getter的区别

无法在nextjs应用程序中通过id从mongoDB删除'

Chrome是否忽略WebAuthentication userVerification设置?""

Next.js(react)使用moment或不使用日期和时间格式

获取Uint8ClampedArray中像素数组的宽度/高度

如何在Angular17 APP中全局设置AXIOS

Jest toHaveBeenNthCalledWith返回当前设置的变量值,而不是调用时的值

从另一个数组中的对应行/键值对更新数组中的键值对对象

WebSocketException:远程方在未完成关闭握手的情况下关闭了WebSocket连接.&#三十九岁;

无法使用npm install安装react-dom、react和next

使用可配置项目创建网格

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

为什么这个最小Angular 的Licial.dev设置不起作用?

如何设置时间选取器的起始值,仅当它获得焦点时?