第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

項(xiàng)目未使用 Redux 從數(shù)組中刪除

項(xiàng)目未使用 Redux 從數(shù)組中刪除

慕村225694 2022-06-05 09:45:50
我正在學(xué)習(xí)一個(gè)嘗試學(xué)習(xí) Redux 的教程。我得到了第一個(gè)動(dòng)作,這是一個(gè)簡(jiǎn)單的 GET API 調(diào)用,但我被困在我試圖創(chuàng)建的下一個(gè)動(dòng)作上。代碼如下所示:在組件中:class ShoppingList extends Component {  componentDidMount() {    this.props.getItems();  }  handleClick = id => {    console.log("component " + id);    this.props.deleteItem(id);  };  render() {    const { items } = this.props.item;    return (      <Container>        <ListGroup>          <TransitionGroup className="shoppingList">            {items.map(({ id, name }) => (              <CSSTransition key={id} timeout={500} classNames="fade">                <ListGroupItem>                  <Button                    className="button1"                    color="danger"                    size="sm"                    onClick={e => this.handleClick(id, e)}                  >                    &times;                  </Button>                  {name}                </ListGroupItem>              </CSSTransition>            ))}          </TransitionGroup>        </ListGroup>      </Container>    );  }}ShoppingList.propTypes = {  getItems: PropTypes.func.isRequired,  item: PropTypes.object.isRequired,  deleteItem: PropTypes.func.isRequired};const mapStateToProps = state => ({  item: state.item});export default connect(mapStateToProps, { getItems, deleteItem })(ShoppingList);在我的減速機(jī)中:const initialState = {  items: [    { id: 3, name: "Eggs" },    { id: 4, name: "Milk" },    { id: 5, name: "Steak" },    { id: 6, name: "Water" }  ]};export default function(state = initialState, action) {  switch (action.type) {    case GET_ITEMS:      return {        ...state      };    case DELETE_ITEM:      console.log("reducer");      return {        ...state,        items: state.items.filter(item => item.id !== action.id)      };    default:      return state;  }}但是,當(dāng)我單擊按鈕嘗試從列表中刪除項(xiàng)目時(shí),沒(méi)有任何反應(yīng)。我可以在 Redux 控制臺(tái)中看到正在調(diào)度該操作,但它似乎沒(méi)有任何效果。有什么建議么?
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

你有在deleteItem行動(dòng){ type, payload }。相反,您可以在 reducer return 語(yǔ)句中擁有{ type, id }或使用。payload


我會(huì)做以下事情 - 所以你通過(guò)id而action不是傳遞payload:


export const deleteItem = id => {

  console.log("actions");

  return {

    type: DELETE_ITEM,

    id

  };

};

或者以后用途的最佳選擇 - 繼續(xù)payload添加id為屬性:


// action

export const deleteItem = id => {

  console.log("actions");

  return {

    type: DELETE_ITEM,

    payload: { id }

  };

};


// reducer

case DELETE_ITEM:

   // here destructuring the property from payload

   const { id } = action.payload;

   return {

      ...state,

      items: state.items.filter(item => item.id !== id)

   };

我希望這有幫助!


查看完整回答
反對(duì) 回復(fù) 2022-06-05
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)