react-native - redux 状态更新缓慢

标签 react-native redux redux-thunk

所以我遇到了变量更新比路由更改慢的问题。 例如,当我的注册 View 出现错误时,该错误会立即显示出来。当按返回键返回登录 View 时,错误正在通过操作重置(在 componentWillUnmount 上触发操作“clearErrors”) 为空字符串。问题是,在收到新的空错误状态之前,我可以在登录时暂时看到错误消息。

ErrorReducer.js

import {
    ERROR,
    CLR_ERROR
} from '../actions/types';

const INIT_STATE = {
    error: ''
};

export default (state = INIT_STATE, action) => {
    switch (action.type) {
        case ERROR:
            return { ...state, error: action.payload };
        case CLR_ERROR:
            return { ...state, error: '' };
        default:
            return state;
    }
};  

error.js( Action )

import { CLR_ERROR } from './types';

export const clearErrors = () => {
    return (dispatch) => {
        dispatch({ type: CLR_ERROR });
    };
};

登录表单.js

import React, { Component } from 'react';
import { View } from 'react-native';
import { Actions } from 'react-native-router-flux';
import { connect } from 'react-redux';
import { emailChanged, passwordChanged, loginUser, resetRoute, autoLogin } from '../actions';
import { Button, Input, Message } from './common';

class LoginForm extends Component {

    componentWillUnmount() {
        this.props.resetRoute();
    }

    onEmailChange(text) {
        this.props.emailChanged(text);
    }

    onPasswordChange(text) {
        this.props.passwordChanged(text);
    }

    onButtonPress() {
        this.props.loading = true;
        const { email, password } = this.props;
        this.props.loginUser({ email, password });
    }

    render() {
        return (
            <View
                style={{
                    flex: 1,
                    marginLeft: 10,
                    marginRight: 10,
                    flexDirection: 'column',
                    justifyContent: 'center',
                    alignItems: 'center'
                }}
                keyboardShouldPersistTaps="always"
                keyboardDismissMode="on-drag"
            >

                <Message
                    type="danger"
                    message={this.props.error}
                />

                <Input
                    placeholder="din@email.se"
                    keyboardType="email-address"
                    returnKeyType="next"
                    onChangeText={this.onEmailChange.bind(this)}
                    value={this.props.email}
                    icon="ios-mail"
                />
                <Input
                    secureTextEntry
                    placeholder="ditt lösenord"
                    onChangeText={this.onPasswordChange.bind(this)}
                    value={this.props.password}
                    icon="ios-key"
                    iconSize={22}
                />

                <Button
                    loading={this.props.loading}
                    uppercase
                    color="primary"
                    label="Logga in"
                    onPress={this.onButtonPress.bind(this)}
                />

                <Button
                    fontColor="primary"
                    label="Registrera"
                    onPress={() => Actions.register()}
                />
                <Button
                    fontColor="primary"
                    label="Glömt lösenord"
                    onPress={() => Actions.resetpw()}
                />

            </View>
        );
    }

}

const mapStateToProps = ({ auth, errors }) => {
    const { email, password, loading, token } = auth;
    const { error } = errors;
    return { email, password, error, loading, token };
};

export default connect(mapStateToProps, {
    emailChanged, passwordChanged, loginUser, resetRoute, autoLogin
})(LoginForm);

Message.js(显示错误的组件)

import React from 'react';
import { View, Text } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import { colors } from '../style';

export const Message = ({ type, message }) => {
    const style = {
        view: {
            alignSelf: 'stretch',
            flexDirection: 'row',
            justifyContent: 'center',
            alignItems: 'center',
            padding: 20,
            margin: 15,
            backgroundColor: colors[type],
            borderRadius: 3,
            elevation: 5,
            shadowRadius: 5,
            shadowColor: colors.smoothBlack,
            shadowOffset: { width: 2.5, height: 2.5 },
            shadowOpacity: 0.5
        },
        text: {
            color: colors.alternative,
            fontSize: 12,
            alignSelf: 'center',
            flex: 1
        },
        icon: {
            marginRight: 20,
            marginLeft: 0,
            marginTop: 2,
            alignSelf: 'center'
        }
    };
    const getIcon = (iconType) => {
        switch (iconType) {
            case 'info':
                return 'ios-information-circle';
            case 'success':
                return 'ios-checkmark-circle';
            case 'danger':
                return 'ios-alert';
            case 'warning':
                return 'ios-warning';
            default:
                return;
        }
    };
    if (message.length > 0) {
        return (
            <View style={style.view}>
                {(type) ? <Icon name={getIcon(type)} size={20} style={style.icon} /> : null}
                <Text style={style.text}>{message}</Text>
            </View>
        );
    }
    return <View />;
};

我在设备 OnePlus3 上运行,删除了所有 console.logs,生产构建。

根据我的阅读,这应该很快。我在这里做错了什么吗?

最佳答案

如果不看你的渲染代码就不可能说,但很可能缓慢不是由 redux 更新状态所花费的时间造成的,而是 React 在调度完成后重新渲染 UI -可能是因为当您的导航器正在转换时它正忙于重新渲染其他东西。

为了保证 redux-thunk 的 Action 顺序,你可以从你的 thunk Action 创建者返回一个 Promise 并等待导航回来,直到 Action 被分派(dispatch):

export const clearErrors = () => {
    return (dispatch) => {
        return new Promise(dispatch({ type: CLR_ERROR }));
    };
};

然后在您看来,一旦错误被清除,您就可以执行后退导航操作:

// assuming the action creator has been passed
// to the component as props
this.props.clearErrors().then(() => navigator.back());

关于react-native - redux 状态更新缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46023071/

相关文章:

javascript - 无法输入 react 输入文本字段 - onChange 无法正常工作

security - React Native redux-persist 加密 key 生成

javascript - 颜色不是从 Prop 渲染的

javascript - react-native firebase 在类 com.facebook.react.bridge.ReadableNativeMap 上找不到要序列化的属性

javascript - 我们如何从 Redux - saga 中的 store.dispatch 返回一个 Promise,以便我们可以等待解析然后在 SSR 中渲染?

reactjs - 你应该如何使用 flowtype redux thunk ?

javascript - 子组件不会重新渲染,但父组件会重新渲染。如何让子组件重新渲染?

reactjs - 获取调度函数返回值

react-native - 如何使用 tcomb-form-native 切换到下一个输入字段?

react-native - 没有 .babelrc 的 Jest 错误 使用它会出现 React Native 错误