简体   繁体   English

使用 Redux 操作处理数据时出错

[英]Error while processing data with Redux actions

I am trying to build filtering system to my React webapp using Redux actions.我正在尝试使用 Redux 操作为我的 React webapp 构建过滤系统。 I think i have set up everything correctly but somehow my data doesn't come through.我想我已经正确设置了一切,但不知何故我的数据没有通过。 Have i formatted something incorrectly?我的格式有误吗? What could be issue here?这里可能有什么问题?

Here is my VisibleTrainingsList.js file that should filter my trainings (as items) based on current filter that is sent with visibilityFilter action:这是我的 VisibleTrainingsList.js 文件,它应该根据使用 visibilityFilter 操作发送的当前过滤器过滤我的培训(作为项目):
VisibleTrainingsList.js: VisibleTrainingsList.js:

import { connect } from 'react-redux';
import TrainingList from '../containers/TrainingListView';
import { VisibilityFilters } from '../store/actions/filter';

constructor() {
        super();
        this.state = {
            trainings: []
        };
}

state = {
        trainings: []
}

    componentDidMount() {
        axios.get('http://127.0.0.1:8000/api/')
            .then(res => {
                this.setState({
                    trainings: res.data
                });
                console.log(res.data);
            })
    }



const getVisibleTrainings = (trainings, filter) => {
    switch (filter) {
        case VisibilityFilters.SHOW_ALL:
            let filteredTrainings = this.state.trainings.filter(
                (trainings) => {
                    return trainings
                }
            );
            return filteredTrainings;
        case VisibilityFilters.SHOW_TARTU:
            let filteredTrainings = this.state.trainings.filter(
                (trainings) => {
                    return trainings.location.toLowerCase().indexOf('tartu') !== -1;
                }
            );
            return filteredTrainings;
        case VisibilityFilters.SHOW_TALLINN:
            let filteredTrainings = this.state.trainings.filter(
                (trainings) => {
                    return trainings.location.toLowerCase().indexOf('tallinn') !== -1;
                }
            );
            return filteredTrainings;

    }
}

const mapStateToProps = state => ({
    filteredTrainings: getVisibleTrainings(state.trainings, state.visibilityFilter)
})

export default connect(mapStateToProps, none)(TrainingList)

Here is actual trainings list that gets trainings from previous file and puts them into another Trainings component as data.这是实际的培训列表,它从以前的文件中获取培训并将它们作为数据放入另一个培训组件中。
TrainingListView.js: TrainingListView.js:

class TrainingList extends React.Component {

    constructor() {
        super();
        this.state = {
            trainings: []
        };
    }


    state = {
        trainings: []
    }

    componentDidMount() {
        axios.get('http://127.0.0.1:8000/api/')
            .then(res => {
                this.setState({
                    trainings: res.data
                });
                console.log(res.data);
            })
    }

    render() {
        return (
        <div>
            <Filter />
            <Trainings data={this.props.filteredTrainings} />

        </div>
        );
    }
}

export default TrainingList;

There are multiple issues with the code you posted (I'm assuming it's just an incomplete copy/paste job), but one thing that jumps out immediately is you are not returning anything from getVisibleTrainings .您发布的代码存在多个问题(我假设它只是一个不完整的复制/粘贴工作),但立即跳出来的一件事是您没有从getVisibleTrainings返回任何内容。 If you return filteredTrainings at the end of the function, that may solve the issue you're seeing.如果您在 function 末尾返回filteredTrainings训练,这可能会解决您看到的问题。

I think you want to pass state.trainings to getVisibleTrainings instead of state.filteredTrainings :我想你想通过state.trainingsgetVisibleTrainings而不是state.filteredTrainings

const mapStateToProps = state => ({
    filteredTrainings: getVisibleTrainings(state.trainings, state.visibilityFilter)
})

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

相关问题 在React / Redux应用程序中在后台获取数据时正确地分配操作的方式(有条件的) - Correct way of dispatching actions (Conditionally) while fetching data in background in react/redux app 为什么在redux中需要&#39;Actions&#39;作为数据? - Why do you need 'Actions' as data in redux? 从组件内部的操作访问数据| 终极版 - Access data from actions inside of components | Redux Redux在reducer之间共享数据和操作的方法 - Redux ways to share data and actions between reducers 反应Redux动作和Reducer映射错误 - React Redux actions and reducer mapping error Actions 必须是普通对象 Error In React 和 Redux - Actions must be plain objects Error In React and Redux Redux Actions 上的错误必须是普通对象 - Error on Redux Actions must be plain objects 显示未捕获的错误:在尝试调度数据以存储在 redux 中时,操作必须是普通对象 - Showing Uncaught Error: Actions must be plain objects, when trying to dispatch data to store in redux 处理路由this.store.findALL时,Ember数据错误不是一个函数 - Ember Data error while processing route this.store.findALL is not a function 无法超越“动作必须是简单的对象。 使用自定义中间件进行异步操作。”尝试遵循redux.js.org教程时出错 - Can't beat the “Actions must be plain objects. Use custom middleware for async actions.” error while trying to follow redux.js.org tutorial
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM