【问题标题】:store.dispatch is not updating the state in redux?store.dispatch 没有更新 redux 中的状态?
【发布时间】:2021-09-13 07:06:24
【问题描述】:

我的组件只有添加和减号按钮。如果我使用来自react-reduxuseDispatch,它将更新redux 状态。问题是当我使用store.dispatch() 时,它不会更新状态。

组件

import React from 'react';
import { SafeAreaView } from 'react-native';
import moment from 'moment';
import { FlatList, Text, View, Button } from 'react-native';
import { Card, Paragraph, Divider } from 'react-native-paper';
import { useSelector, useDispatch } from 'react-redux';
import AppContainer from '../Common/AppContainer';
import LinearButton from '../Common/Button';
import translation from '../../Util/Translation';
import styles from '../../Components/Styles/Notification/Notification';
import NotificationAction from '../../Redux/Notification/Actions';
import createStore from '../../Redux/CreateStore';

const { t } = translation;

const NotificationScreen = (props) => {
  const { NotificationDetails, onSendNotificationPress } = { ...props };
  const count = useSelector((state) => state.notificationReducer);
  const { store } = createStore();
  const dispatch = useDispatch();
  const addPress = () => {
    store.dispatch(NotificationAction.add(23));
  };
  return (
    <SafeAreaView style={styles.container}>
      <AppContainer title={t('NOTIFICATION')} leftIcon="ios-arrow-back" isLeft>
        <FlatList
          data={NotificationDetails.sort((_val, $val) => new Date($val.data) - new Date(_val.data))}
          keyExtractor={(item, i) => i.toString()}
          showsVerticalScrollIndicator={false}
          ItemSeparatorComponent={() => <Divider />}
          renderItem={({ item }) => {
            return (
              <View style={styles.fltLst}>
                <View style={styles.flt}>
                  <View style={styles.drtn}>
                    <Text style={styles.hdr}>Notification</Text>
                    <Paragraph> {moment(item.data).startOf().fromNow()}</Paragraph>
                  </View>
                  <Text>{item.Subscriber_Status}</Text>
                </View>
              </View>
            );
          }}
        />
      </AppContainer>
      <View style={styles.buttonView}>
        <View style={styles.button}>
          <LinearButton title={t('SENDNOTIFICATION')} onPress={onSendNotificationPress} />
        </View>
        <Button title="Add" onPress={addPress} />
        <Text style={[styles.hdr, { alignSelf: 'center' }]}> {count}</Text>
        <Button title="Remove" />
      </View>
    </SafeAreaView>
  );
};

export default NotificationScreen;

【问题讨论】:

    标签: react-native redux react-redux


    【解决方案1】:

    你不需要写store.。只需使用dispatch(actions.someFunc(params))

    https://react-redux.js.org/api/hooks#usedispatch

    【讨论】:

    • 我想调度操作而不是 useDispatch
    • 注意文档中对这个钩子的引用:“这个钩子从 Redux 存储返回一个对调度函数的引用。你可以根据需要使用它来调度动作。”
    【解决方案2】:
    const dispatch = useDispatch();
    ...
    // then further
    dispatch(NotificationAction.add(23))
    

    您必须导入您在StoreProvider 中传递的同一商店 在您的问题中,您正在逐字创建一个新的 store 实例,如下所示: const { store } = createStore(); 您不应创建商店的多个实例。如果您有多个实例,则该值将在任何一个实例中更新,而不是在您为提供者传递的那个实例中更新。

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 2021-03-21
      • 1970-01-01
      • 2018-03-08
      • 2019-05-15
      • 2019-03-26
      • 2020-05-23
      • 2018-07-21
      相关资源
      最近更新 更多