【发布时间】:2020-11-10 09:14:34
【问题描述】:
我在 thunk 中间件的帮助下通过映射来自 JSON 响应的数据来动态地在组件文件中创建一个列表。 我想从该列表中选择元素并将它们添加到“我的收藏夹”中, 我的 JSON 响应没有唯一的 ID,而是唯一的字符串。
减速机:
const initialstate = { isFav: false }
const reducer = (state=initialstate, action) => {
switch(action.type){
case actionTypes.TOGGLE_FAVORITE:
return {
...state,
isFav: **What to do here?**
}
default:
break;
}
}
export default reducer;
Action.js:
export const TOGGLE_FAVORITE = 'TOGGLE_FAVORITE';
export const togglefav = (url) =>{
return{
type: TOGGLE_FAVORITE,
payload: url
}
}
组件.js
this.props.dailySource.map((source,index) =>{
...
<div className={classes.star}>
<span className="fa fa-star" key={index} onClick={()=>
this.props.onToggleFavorite(source.url) }
style={{ color: (this.props.toggleFav) ? 'red' : '' }}></span>
</div>
}
}))
const mapStateToProps = state =>{
return{
dailySource: state.list.newsItem,
toggleFav: state.list.isFav
}
}
const mapDispatchToProps = dispatch =>{
return{
onToggleFavorite: (url) => dispatch (actionCreators.togglefav(url))
}
}
【问题讨论】:
标签: javascript reactjs redux react-redux reducers