Javascript Redux/React:映射到存储时调度操作
我有一个父组件Javascript Redux/React:映射到存储时调度操作,javascript,reactjs,redux,Javascript,Reactjs,Redux,我有一个父组件CardList,它映射到redux存储,并为存储中找到的每个数据集呈现一个Card组件 我的问题是我无法发送操作来更新存储 我尝试在父级中分派addLine()(如下所示),也直接在Card中尝试。两者都没有执行任何操作或启动了操作,之后我遇到了一个错误无法读取未定义的属性“map” 卡片列表: (...) function CardList() { const recipes = useSelector((recipes) => recipes); const
CardList
,它映射到redux存储,并为存储中找到的每个数据集呈现一个Card
组件
我的问题是我无法发送操作来更新存储
我尝试在父级中分派addLine()
(如下所示),也直接在Card
中尝试。两者都没有执行任何操作或启动了操作,之后我遇到了一个错误无法读取未定义的属性“map”
卡片列表:
(...)
function CardList() {
const recipes = useSelector((recipes) => recipes);
const dispatch = useDispatch();
return (
<Link to='/recipe'>
<ul>
<section className='cardListWrapper'>
{recipes.map((recipe) => (
<Card
key={recipe.id}
recipe={recipe}
onClick={() => dispatch(addLine())}
/>
))}
</section>
</ul>
</Link>
);
}
(...)
(...)
function Card({ recipe }) {
const { title, text } = recipe;
return (
<div className='cardWrapper'>
<h4 className='cardTitle'>{title}</h4>
<p className='cardText'>{text}</p>
<button className='cardButton'>READ MORE</button>
</div>
);
}
(...)
// action:
export function addLine() {
return {
type: "ADDLINE",
};
}
// reducer:
function reducer(recipes = initialState, action) {
switch (action.type) {
case "ADDLINE":
return console.log("this action fired");
default:
return recipes;
}
}
还原程序返回了无效值。试一试
function reducer(recipes = initialState, action) {
switch (action.type) {
case "ADDLINE":
// the problem was here
console.log("this action fired");
return recipes;
default:
return recipes;
}
}
啊,我看到了我的错误,我没有返回
配方。谢谢你指给我看。