Search code examples
react-nativereduxflatlist

How to re render FlatList React Native when working with redux


I am new to React Native, I'm having trouble with FlatList. I assign extraData to a redux value but it does not re-render the FlatList when redux update. I think I should assign it to a local state, but I got too many re render error. Many thanks to anyone who help me! Below is my code

function CartScreen() {
const cart = useSelector((state) => state.cartReducer);
console.log(cart);
const dispatch = useDispatch();

useEffect(() => {
    dispatch(cartActions.refreshCartAction(cart));
}, []);

const renderItem = ({ item }) => {
    return <CartItem book={item} />;
};

return (
    <View>
        <FlatList
            data={cart}
            renderItem={renderItem}
            keyExtractor={(item) => item._id.toString()}
            extraData={cart}
        />
    </View>
);
}

export default CartScreen;

Solution

  • I found an error in my redux action that solve the problem. Thank everyone!!