首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >反应本机剩余(Saga)初始状态不起作用

反应本机剩余(Saga)初始状态不起作用
EN

Stack Overflow用户
提问于 2022-01-31 17:57:33
回答 1查看 125关注 0票数 0

我正在创建Reactive原住民应用,与Redux。虽然一切都在运行,但初始状态未被填充,调度也不起作用。请帮帮忙。

App.tsx

代码语言:javascript
复制
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import saga from 'redux-saga';
 
import { appReducer }  from './src/store/reducers';
import rootSaga from './src/store/sagas';
import { Counter } from './src/components/counter/counter';
 
// The middlewares which will be used in this App
const middlewares = [] as any;

// Initialize the saga middleware
const sagaMiddleware = saga();

middlewares.push(sagaMiddleware);

const store = createStore(
  appReducer,
  applyMiddleware(...middlewares)
);

sagaMiddleware.run(rootSaga);

export const App = () => {
    return (
      <Provider store={store}>
        <Counter />
      </Provider>
    );
}

index.ts (减速器)

代码语言:javascript
复制
import { ActionType } from 'typesafe-actions';
import { combineReducers } from 'redux';
import { CounterActionTypes, CounterState } from '../../constants/action-types';
import  { counterReducer } from './counterReducer';

// The top-level state object
export interface ApplicationState {
    readonly counter: CounterState 
}

export type CounterAction = ActionType<typeof CounterActionTypes>

export const appReducer = () => combineReducers({
    counter: counterReducer
});

counterReducer.ts

代码语言:javascript
复制
import { CounterActionTypes, CounterState } from '../../constants/action-types';
import { Reducer } from 'redux';
import { ActionType } from 'typesafe-actions';
import { counterActions } from '../actions/index';
 
export type CounterActions = ActionType<typeof counterActions>;


export const counterInitialState: CounterState = {
  count: 1000,
 };


export const counterReducer: Reducer<CounterState, CounterActions> = (
  state = counterInitialState,
  action,
): CounterState => {
  switch (action.type) {
    case CounterActionTypes.INCREASE:
      return {
        ...state,
        count: state.count + 1
      };
    case CounterActionTypes.DECREASE:
      return {
        ...state,
        count: state.count - 1
      };
    default: 
      return state;
   
  }
};

counter.tsx (组件计数器状态总是未定义)

代码语言:javascript
复制
import {
  Button,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import { increase } from '../../store/actions/counterActions';
import { ApplicationState } from '../../store/reducers';
import React, { useState }  from 'react';


export const Counter = () =>  {

  const  count = useSelector((state: ApplicationState) => state.counter?.count);
  
  const dispatch = useDispatch();

  return  (<>
    <View style={{alignItems: 'center', justifyContent: 'center', width: 30,}}>
      <TouchableOpacity>
        <Text>-{count}-</Text>
        
      </TouchableOpacity>
        <Button  title='Click here to increase' onPress={() => dispatch(  increase())} ></Button>
      <TouchableOpacity>
        <Text>async up</Text>
      </TouchableOpacity>
    </View>  
    </>)
}

我不得不把state.counter?.count放进去,这样它就不会坏了。它应该有初始值,但它没有?为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-31 19:08:44

我只看了一下代码,但我认为问题在于您的appReducer。我不认为它应该被包装在一个函数中。

因此,与其:

代码语言:javascript
复制
export const appReducer = () => combineReducers({
    counter: counterReducer
});

尝试:

代码语言:javascript
复制
export const appReducer = combineReducers({
    counter: counterReducer
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70930648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档