在这段代码中,我使用了REACTION-REDUX和REPACT-RUTER.Reaction-Redux版本是旧的,但我想知道我在这段代码中哪里出错了(在这个版本的Reaction-Redux和Reaction-Router中我指的是). 我try 在main.jsx中得到ingredients,并在OrderSummary组件中使用它,但我得到了如下错误:
- 无法读取未定义的属性,
- 找不到州/自治区.
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]
};