简体   繁体   English

Redux - Redux 存储中的数据未更新

[英]Redux - Data not updating in Redux store

I'm having trouble passing some data fetched via axios into the Redux store to be used on another component.我无法将通过 axios 获取的一些数据传递到 Redux 存储以用于另一个组件。 I have other actions and reducers that are very similar and are working, but this one isn't.我有其他非常相似并且正在工作的动作和减速器,但这个不是。

The dataflow is as follows:数据流如下:

  1. Start in Filter component从过滤器组件开始
  2. componentDidMount fires and fetches data thru axios componentDidMount 触发并通过 axios 获取数据
  3. updateInfo (action) is called to update Redux store with the response's data调用 updateInfo (action) 以使用响应的数据更新 Redux 存储
  4. This is where it acts incorrectly.这是它行为不正确的地方。 It falls into the default switch case statement instead of UPDATE_APPT_LIST action.它属于默认的 switch case 语句,而不是 UPDATE_APPT_LIST 操作。
  5. Info component console logs props but only the other reducers can be found.信息组件控制台记录道具,但只能找到其他减速器。
// actions/index.js
export const UPDATE_APPT_LIST = "UPDATE_APPT_LIST" 

export const updateInfo = list => {
    return {
        type: UPDATE_APPT_LIST,
        list
    }
}
// reducers/index.js
import { UPDATE_APPT_LIST } from '../actions'

const updateInfoReducer = (list = [], action) => {
    switch (action.type) {
        case UPDATE_APPT_LIST:
            return action.list;
        default:
            return list;
    }
}
// Filter.js
class Filter extends React.Component {
    state = { info: [] }

    async componentDidMount() {
        await this.retrieveInfo()
    }

    retrieveInfo = async () => {
        let res = await axios.get('someURL')
        updateInfo(res.data)  // update redux store function
        this.setState({ info: res.data })
    }
}

mapStateToProps = state => {
    return { infoList: updateInfoReducer }
}

export default connect(mapStateToProps, { updateInfo })(Filter)
// Info.js
class Info extends React.Component {
    render() {
        console.log(this.props)
        return (... some jsx)
    }
}

mapStateToProps = state => {
    return state
}

export default connect(mapStateToProps)(Info)

Found the answer and it's a silly one.找到了答案,这是一个愚蠢的答案。 I forgot const { updateInfo } = this.props in Filter.js.我忘记了 Filter.js 中的const { updateInfo } = this.props this.props。 This would lead the action to be dispatched anyways but action.type did not match in the reducer, leading the default case to be fired.无论如何,这将导致 action 被调度,但action.type在 reducer 中不匹配,导致默认情况被触发。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM