簡體   English   中英

Redux - Redux 存儲中的數據未更新

[英]Redux - Data not updating in Redux store

我無法將通過 axios 獲取的一些數據傳遞到 Redux 存儲以用於另一個組件。 我有其他非常相似並且正在工作的動作和減速器,但這個不是。

數據流如下:

  1. 從過濾器組件開始
  2. componentDidMount 觸發並通過 axios 獲取數據
  3. 調用 updateInfo (action) 以使用響應的數據更新 Redux 存儲
  4. 這是它行為不正確的地方。 它屬於默認的 switch case 語句,而不是 UPDATE_APPT_LIST 操作。
  5. 信息組件控制台記錄道具,但只能找到其他減速器。
// 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)

找到了答案,這是一個愚蠢的答案。 我忘記了 Filter.js 中的const { updateInfo } = this.props this.props。 無論如何,這將導致 action 被調度,但action.type在 reducer 中不匹配,導致默認情況被觸發。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM