我正在使用altjs作为我的Flux实现来构建React应用程序。当我尝试从前端创建/删除项目时,无论我将什么作为参数传递给create / delete函数,它总是以传递整个状态结束。

例如:我正在尝试删除id = 1的项目。我在该项目上单击删除,然后仅将ID传递给组件中的删除功能。该函数再次传递ID来调用删除服务。一旦到达存储层,它就具有组件的整个状态,而不仅仅是传递的ID。

我对React / Flux还是很陌生,不确定我做错了什么或为什么会这样。

主要组件删除功能:

deleteItem = (id) => {
        console.log(id) //logs out 56b36c34ad9c927508c9d14f
        QuestionStore.deleteQuestion(id);
    }


此时,id仍然只是id。

QuestionStore:

import alt from '../core/alt';
import QuestionActions from '../actions/QuestionActions';
import QuestionSource from '../sources/QuestionSource';

class QuestionStore {
    constructor() {
        this.bindActions(QuestionActions);
        this.exportAsync(QuestionSource);
        this.loaded = false;
        this.modalIsOpen = false;
        this.data = [];
        this.question = {
            "text": '',
            "tag": [],
            "answer": [],
            "company": [],
            "createdAt": ''
        };
        this.error = null;
        this.questionAdded = null;
        this.questionDeleted = null;
    }

    onGetQuestions(data) {
        if (data === false) {
            this.onFailed();
        } else {
            this.data = data;
            this.loaded = true;
        }
    }

    onCreateQuestion(response) {
        if (response === false) {
            this.onFailed();
        } else {
            this.questionAdded = response;
        }
    }

    onDeleteQuestion(response) {
        if (response === false) {
            this.onFailed();
        } else {
            this.questionDeleted = response;
        }
    }

    onFailed(err) {
        this.loaded = true;
        this.error = "Data unavailable";
    }
}

export default alt.createStore(QuestionStore, 'QuestionStore');


QuestionSource:

import Api from '../services/QuestionApi';
import QuestionActions from '../actions/QuestionActions';

let QuestionSource = {
    fetchData() {
        return {
            async remote(state) {
                return Api.getQuestions()
            },
            success: QuestionActions.getQuestions
        }
    },

    createQuestion(question) {
        return {
            async remote(question) {
                return Api.createQuestion(question)
            },
            success: QuestionActions.createQuestion
        }
    },

    deleteQuestion(id) {
        //id here is an object of the entire state of QuestionStore
        return {
            async remote(id) {
                return Api.deleteQuestion(id)
            },
            success: QuestionActions.deleteQuestion
        }
    }
};

export default QuestionSource;


一旦到达这一点,即使仅传递了id,id仍是组件的整个状态。

最佳答案

绑定到操作的第一个参数是商店的状态(exportAsync调用结果的一部分。因此,所有参数都向右移动一个,调用函数所用的第一个参数又变成第二个参数,请参见下面的代码示例:

deleteQuestion(state, id) {
    //state here is an object of the entire state of QuestionStore
    //id will be the first argument you provide to the function.
    return {
        async remote(id) {
            return Api.deleteQuestion(id)
        },
        success: QuestionActions.deleteQuestion
    }
}


Documentation from alt.js about handling async operations.

09-25 17:25
查看更多