私はネイティブの反応を経由してReduxの中にクリックすることにより、1によってFlatList 1から項目を削除することはできません

roz333:

私は、以下を含む3つの画面があり、単純なショッピングアプリを作成しました:HomeBook StoreおよびCartスクリーン

私はすべてが一つのことを除いてスムーズに動作し、すべての状態を更新するためのReduxのを使用しています。でアイテムをクリックしたとき、私は、ユーザーが欲しいCart、彼らは1で取り外したものを得るが、問題は、私は1つのアイテムをクリックしたときにそれらのすべてを同時に除去してしまうことがあります

私はこれをどのように修正することができますか?

減速コード:


import {
    ADD,
    REMOVE


} from '../actions/types';



const initialState = {

    items: [],
    counter: 0
}



export const cardReducer = (state = initialState, action) => {

    switch (action.type) {
        case ADD:
            return {
                ...state, items: state.items.concat({

                    name: action.payload.name,
                    index: Math.random(),
                    price: action.payload.price,
                    id: action.payload.id



                }), counter: state.counter + 1 }
            break;
        case REMOVE:
            return {
                ...state,
                items: state.items.filter((item) => {

                    item.index !== action.payload

                }), counter: state.counter - 1 }

            break;

        default:
            return state;
    }}

アクションコード:

import {
    ADD,
    REMOVE


} from './types';


export const addSomething = (item) => {

    return {

        type: ADD,
        payload: item

    }}


export const removeSomething = (index) => {

    return  {


            type: REMOVE,
            payload: index
        } }

そして、これはあるCartスクリーンコード:

import { useDispatch, useSelector } from 'react-redux';
import { addSomething, removeSomething } from '../actions';




const Cards = (props) => {


  const { navigation } = props;
  const dispatch = useDispatch();
  const items = useSelector(state => state.cardR.items)



  return (


      <View style={{ alignItems: 'center', flex: 1 }}>
        <View style={{ width: '80%' }}>
          <FlatList
            data={items}
            keyExtractor={(item, index) => index}
            renderItem={({ item, index }) => (

              <TouchableOpacity
                style={styles.button}
                activeOpacity={0.7}
                onPress={(index) => dispatch(removeSomething(item.index))}

              >

                <Text style={{ color: 'white' }}>{item.name}</Text>
                <Text style={{ color: 'white' }}>{item.price}</Text>

              </TouchableOpacity>

            )} />
        </View>
      </View>


  )}

KundanシンChouhan:

問題がでそうですindex同様に@giotskhada彼の応答で述べた、プロパティ。可能な解決策は、その有する取り外し可能な項目をチェックすることができid、一意の各項目を識別するために、既に存在する代わりにインデックス。

代わりにこれを試してみてください -

減速コード:

case REMOVE:
    return {
        ...state,
        items: state.items.filter((item) => {
            return item.id !== action.payload   // Id instead of index and return statement
        }), 
        counter: state.counter - 1 
    }

    break;

カート画面コード:

<TouchableOpacity
    style={styles.button}
    activeOpacity={0.7}
    onPress={(index) => dispatch(removeSomething(item.id))}>  // id instead of index

        <Text style={{ color: 'white' }}>{item.name}</Text>
        <Text style={{ color: 'white' }}>{item.price}</Text>

</TouchableOpacity>

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=33527&siteId=1