【问题标题】:Use redux action the dispatch is not working使用 redux 操作调度不起作用
【发布时间】:2019-10-28 04:24:01
【问题描述】:

我已经合并了我的 react redux。

这是我的 App.js

import React from 'react';
import ReduxThunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { compose, createStore, applyMiddleware } from 'redux';
import reducers from './src/reducers';
import AppContainer from './src/navigator'

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const App: () => React$Node = () => {

  const store = createStore(reducers, {}, composeEnhancers(applyMiddleware(ReduxThunk)));

  return (
    <Provider store={store}>
      <AppContainer />   
    </Provider>     
  );
};

export default App;

src/reducers/index.js

import { combineReducers } from 'redux';
import LoginReducer from './LoginReducer';

export default combineReducers({
  LoginRedux: LoginReducer
});

如果我使用我的操作login(),我可以看到login action start,但我看不到dispatch start

    import React from 'react';
    import { 
      Text, 
      View, 
      TouchableOpacity,
    } from 'react-native';
    import { connect } from 'react-redux';
    import { login } from '../actions';

    const LoginScreen = ({ navigation }) => {

      // console.log('see my test value', testValue)

      return (
        <View>
          <TouchableOpacity 
            onPress={() => {
              login();
            }
          }>
            <View>
              <Text>LOGIN</Text>
            </View>
          </TouchableOpacity>

        </View>
       </View>
      );
    }

    const mapStateToProps = (state) => {
      const { testValue } = state.LoginRedux;
      console.log('mapStateToProps testValue =>', testValue);
      return { testValue };
    };

export default connect(mapStateToProps, { login })(LoginScreen);

如果我console.log(dispatch),会显示dispatch is not defined。

import { LOGIN } from './types';

export const login = () => {
  console.log('login action start')
  return (dispatch) => {
    console.log('dispatch start');
    // console.log(dispatch);
    dispatch({ type: LOGIN, testValue: 'I am test' });
  };  
};

src/reducers/LoginReducer.js

import { LOGIN } from '../actions/types';

const INITIAL_STATE = {
  testValue: ''
};

export default (state = INITIAL_STATE, action) => {
  console.log('reducer =>', action); // I can't see the console.log
  switch (action.type) {
    case LOGIN:
      return {
        ...state,
        testValue: action.testValue
      };
    default:
      return state;
    }
};

我不知道为什么我的动作调度不起作用。我是不是设置错了?

任何帮助将不胜感激。

根据 Zaki Obeid 的帮助,我更新如下: 动作代码:

export const login = () => { 
  console.log('login !');
  return { type: LOGIN }; 
}; 

功能组件代码:

import { login } from '../../actions';

export const SettingScreen = ({ navigation, login }) => {
  // return view code
}

const mapDispatchToProps = dispatch => ({
  // you will use this to pass it to the props of your component
  login: () => dispatch(login),
});

connect(null, mapDispatchToProps)(SettingScreen);

【问题讨论】:

    标签: react-native redux react-redux redux-thunk


    【解决方案1】:

    在 LoginScreen 组件中

    您需要添加 mapDispatchToProps

    const mapDispatchToProps = dispatch => ({
      // you will use this to pass it to the props of your component
      login: () => dispatch(login()),
    });
    
    
    export default connect(mapStateToProps, mapDispatchToProps)(LoginScreen);
    

    那么

    你需要从道具中解构为:

    const LoginScreen = ({ navigation, login }) => {
      // your code
    }
    

    在 actions.js 中

    您在此处使用 dispatch 的方式需要一个库 redux-thunk,它用于异步调用。

    正常的操作应该为您完成这项工作:

       export const login = () => ({
         type: LOGIN,
         testValue: 'I am test' 
    })
    

    我希望这是有用的,并会解决你的问题, 祝你有美好的一天。

    【讨论】:

    • 感谢您的帮助,但是如何在 login() 中触发另一个逻辑?以您的方式,login() 只是一个普通对象。
    • 是的,但是如果我想在操作端添加一些逻辑代码。这是不可能的吧?因为login() 只做一件事将数据发送到reducer。
    • 你可以这样传递:&lt;TouchableOpacity onPress={() =&gt; { login } }&gt;
    • 你可以在动作中写下你的逻辑,但要确保它将这个对象返回给reducer。 ` export const login = () => { // 你的逻辑 return { type: LOGIN, testValue: "I am test" } }; `
    • 再次感谢,我尝试了您的代码,但仍然无法登录,我更新了问题中的代码。 lz可以看一下吗?我什至注释掉import { login } from '../../actions'; 仍然是同样的错误。
    【解决方案2】:

    在 react-redux 应用程序中,您可以通过直接获取 store 对象 (store.dispatch) 或通过 react-redux connect 函数获取调度函数,该函数将调度作为参数提供给你编写的一个函数,然后连接到一个组件

    import { connect } from 'react-redux';
    
    const mapStateToProps = ...
    
    const mapDispatchToProps = (dispatch) => {
        return {
            someHandle: () => dispatch(myActionCreator())
        }
    }
    
    export const connect(mapStateToProps, mapDispatchToProps)(MyComponent)
    

    你不能凭空调用 dispatch —— 它不是一个全局函数。

    【讨论】:

    • 感谢您的回复,但我已将我的操作放入export default connect(mapStateToProps, { login })(LoginScreen); ,我的login() return (dispatch) => { // 一些代码... } ,它不正确?
    • 如果我使用扩展组件而不是我的函数组件并使用this.props.login(),它就可以工作。但是我现在如何在我的函数组件中使用它呢?
    • 我知道我错过了。我之前没有看到为 mapDispatchToProps 传入的对象。也许试试dispatch =&gt; ({ login }) 而不是{ login }
    • 对不起,我对 dispatch =&gt; ({ login }) 而不是 { login } 感到困惑。你的意思是我必须调整我的login() 代码?怎么样?
    • 我修复了代码并且现在可以工作了。非常感谢浮士德。
    【解决方案3】:

    看来您是直接使用登录功能。你将不得不使用道具。只需更改名称即可混淆并通过道具使用。

    import { combineReducers } from 'redux';
    import LoginReducer from './LoginReducer';
    
    export default combineReducers({
      LoginRedux: LoginReducer
    });
    If I use my action login(), I can see login action start, but I can't see dispatch start
    
        import React from 'react';
        import { 
          Text, 
          View, 
          TouchableOpacity,
        } from 'react-native';
        import { connect } from 'react-redux';
        import { login } from '../actions';
    
        const LoginScreen = ({ navigation, userLogin }) => {
    
          // console.log('see my test value', testValue)
    
          return (
            <View>
              <TouchableOpacity 
                onPress={() => {
                  userLogin();
                }
              }>
                <View>
                  <Text>LOGIN</Text>
                </View>
              </TouchableOpacity>
    
            </View>
           </View>
          );
        }
    
        const mapStateToProps = (state) => {
          const { testValue } = state.LoginRedux;
          console.log('mapStateToProps testValue =>', testValue);
          return { testValue };
        };
    
    export default connect(mapStateToProps, { userLogin:login })(LoginScreen);
    

    【讨论】:

    • 感谢您的帮助,但我尝试代码显示 userLogin 不是函数。
    猜你喜欢
    • 2018-05-13
    • 2020-07-11
    • 2017-05-25
    • 2020-05-30
    • 2019-11-12
    • 2018-05-30
    • 2019-09-28
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多