简体   繁体   English

尽管所有数据都正确传递,Redux状态仍未更新

[英]Redux state not updating, despite all data being passed around correctly

Tearing my hair out on this one, and I'm sure it'll be a stupid oversight but I've spent hours on it now! 在这一个上撕掉我的头发,我确信这将是一个愚蠢的疏忽,但我现在花了几个小时!

I'm using the action creators and reducers below. 我正在使用下面的动作创建者和缩减者。 Everything seems to work (I've run a console.log at every stage in the process and the data is being passed around correctly) but for some reason my store state is not updating. 一切似乎都有效(我在进程的每个阶段运行一个console.log ,数据正在正确传递)但由于某种原因我的存储状态没有更新。

The initial state is being set correctly when the page loads, so it's just when I pass data in through the action later on - nothing happens! 页面加载时正在正确设置初始状态,所以当我稍后通过操作传递数据时 - 没有任何反应!

Can anyone spot the error? 谁能发现错误? Thanks! 谢谢!

displayMessage.js (action) displayMessage.js(action)

export default function displayMessage(type, message){
    return {
        type: 'DISPLAY_MESSAGE',
        payload: {
            status: type,
            message: message
        }
    }
}

reducer_displayMessage.js reducer_displayMessage.js

const initialState = {
    status: '',
    message: ''
}

export default (state = initialState, action) => {
    switch (action.type){
        case 'DISPLAY_MESSAGE':
            return action.payload;
    }
    return state;
}

rootReducer.js rootReducer.js

import { combineReducers } from 'redux';
import SlideOverReducer from './reducer_slideOver';
import WorkshopSelectReducer from './reducer_workshopSelection';
import UserDetailsReducer from './reducer_userDetails';
import QualDetailsReducer from './reducer_qualDetails';
import DisplayMessageReducer from './reducer_displayMessage';

export default combineReducers({
    slideOver: SlideOverReducer,
    workshopSelection: WorkshopSelectReducer,
    userDetails: UserDetailsReducer,
    qualDetails: QualDetailsReducer,
    displayMessage: DisplayMessageReducer
});

EDIT: And here's one of the components that should use this action: 编辑:这是应该使用此操作的组件之一:

import React, {Component} from 'react';
import styles from '../../cssPartials/slideOverBar.css';
import tableStyles from '../../cssPartials/formElements.css';
import newUser from '../../../../data/actions/newUser';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import actions from '../../../../data/actions/actions';

class NewUser extends Component {

    constructor(props){
        super(props)

        this.state = {
            type: null,
            firstName: '',
            lastName: '',
            email: ''
        }
    }

    //...Other methods      

    submitForm(e){
        e.preventDefault();

        const firstName = this.state.firstName;
        const lastName = this.state.lastName;
        const email = this.state.email;
        const type = this.state.type;

        newUser(firstName, lastName, email, type, (data) => {
            const displayType = data.success ? 'success' : 'error'
            this.props.displayMessage(displayType, data.message) //Redux dispatch is called here
        });

    }

    render() {

        return (
            <div>
                <form className={styles.formElement}>
                    <div className={`btn-group btn-group-justified`}>
                        <div className={`btn-group`}>
                            <button className={this.state.type === 'admin' ? `btn btn-default active` : `btn btn-default`}
                                    onClick={(e) => this.chooseType(e, 'admin')}>Admin</button>
                        </div>
                        <div className={`btn-group`}>
                            <button className={this.state.type === 'tutor' ? `btn btn-default active` : `btn btn-default`}
                                    onClick={(e) => this.chooseType(e, 'tutor')}>Tutor</button>
                        </div>
                    </div>
                    {
                        this.state.type !== null 
                            ? <div className={styles.spacer}>
                                <input 
                                     type='text'
                                     className={tableStyles.input}
                                     value={this.state.firstName}
                                     placeholder='Enter first name'
                                     onChange={(e) => this.enterName(e, 'first')} />
                              </div>
                            : null

                    }
                    {
                            this.state.type !== null && this.state.firstName.length > 0
                                ? <div className={styles.spacer}>
                                    <input 
                                         type='text'
                                         className={tableStyles.input}
                                         value={this.state.lastName}
                                         placeholder='Enter last name'
                                         onChange={(e) => this.enterName(e, 'last')} />
                                  </div>
                                : null
                    }
                    {
                        this.state.type !== null && this.state.firstName.length > 0 && this.state.lastName.length > 0
                            ? <div className={styles.spacer}>
                                <input type='email'
                                     className={tableStyles.input}
                                     value={this.state.email}
                                     placeholder='Enter e-mail'
                                     onChange={(e) => this.enterEmail(e)} />
                              </div>
                            : null
                    }

                    {
                        this.state.type !== null && this.state.firstName.length > 0 && this.state.lastName.length > 0 && this.state.email.length > 7
                            ? <div className={styles.spacer}>
                                <div className={`btn-group btn-group-justified`}>
                                    <div className={`btn-group`}>
                                        <button type='submit' className={`btn btn-primary`} onClick={(e) => this.submitForm(e)} >Create User Account</button>
                                    </div>
                                </div>
                              </div>
                            : null


                    }

                </form>
            </div>
        )
    }

}

function mapDispatchToProps(dispatch){
    return bindActionCreators({ displayMessage: actions.displayMessage }, dispatch)
}


export default connect(null, mapDispatchToProps)(NewUser);

displayMessage ,你需要做这个.dispatch({type and value ...})对你的reducer说:“嘿,伙计,这里有新东西给你!”

OK, so it looks like there isn't actually any problem at all! 好的,所以看起来根本没有任何问题!

For some reason, the store state shown in the React Developer tools was not updating straight away - when I triggered a separate event which called a different action, the state for this action updated as well. 出于某种原因,React Developer工具中显示的存储状态不会立即更新 - 当我触发一个调用不同操作的单独事件时,此操作的状态也会更新。

I don't understand why it isn't showing immediately - other changes to the state do - but I've tested it with a dummy component that relies on changes to the store state and it seems to work fine. 我不明白为什么它没有立即显示 - 状态的其他更改 - 但我用虚拟组件测试它依赖于存储状态的更改,它似乎工作正常。

So apologies to all - it seems as though this was a non-issue - but it boils down to a bit of strange behaviour in the developer tools rather than an actual problem! 所以对所有人道歉 - 似乎这不是一个问题 - 但它归结为开发人员工具中的一些奇怪行为而不是实际问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Redux 没有正确更新状态——尽管在之前的行中添加了一个对象,但状态仍保持为空数组 - Redux not updating state correctly -- state stays as empty array despite adding an object in the line prior 尽管Redux devtools正确显示了存储,但Redux状态仍返回未定义状态 - Redux state returns as undefined despite Redux devtools correctly displaying store React/Redux - 尽管 Redux 状态发生变化,但组件没有更新 - React/Redux - Component not updating despite Redux state change React Redux数据未传递给道具 - React Redux Data not being passed to props 获取数据并更新 isLoading 但 setState 没有正确更新,它总是最终成为初始 isLoading state - Fetching data and updating the isLoading but setState isn't updating correctly and it always ends up being the initial isLoading state 正确“更新” redux应用程序中的所有状态 - “Updating” all state in redux app properly Redux,获取所有状态而不是我通过的状态 - Redux , Getting all the state instead of the state that i passed 尽管在 React Highcharts 点击事件中进行了更新,但仍显示旧的 state - Old state is being shown despite updating in React Highcharts click event 在Redux thunk中创建的数组未正确传递到状态树 - Array created in a Redux thunk is not being passed properly to the State tree state 中传递的更多数据,未正确获取 - more data passed in state, it is not taken correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM