React CRUD 操作总是使用状态
React CRUD operations always using state
我正在构建一个 React 应用程序,使用 altjs 作为我的 Flux 实现。当我尝试从前端 create/delete 一个项目时,无论我将什么作为参数传递给 create/delete 函数,它总是以传递整个状态而告终。
例如:我正在尝试删除 id=1 的项目。我在该项目上单击删除,然后仅将 id 传递给组件中的删除函数。该函数再次调用删除服务并传递 id。一旦到达存储层,它就会拥有组件的整个状态,而不仅仅是传递的 id。
我对 React/Flux 还是很陌生,不确定我做错了什么或为什么会这样。
主要组件删除功能:
deleteItem = (id) => {
console.log(id) //logs out 56b36c34ad9c927508c9d14f
QuestionStore.deleteQuestion(id);
}
此时id仍然只是id。
问题库:
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');
问题来源:
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
调用结果的一部分。所以所有参数都向右移动一个,你调用的第一个参数函数 with 依次成为第二个参数。请参见下面的代码示例:
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
}
}
我正在构建一个 React 应用程序,使用 altjs 作为我的 Flux 实现。当我尝试从前端 create/delete 一个项目时,无论我将什么作为参数传递给 create/delete 函数,它总是以传递整个状态而告终。
例如:我正在尝试删除 id=1 的项目。我在该项目上单击删除,然后仅将 id 传递给组件中的删除函数。该函数再次调用删除服务并传递 id。一旦到达存储层,它就会拥有组件的整个状态,而不仅仅是传递的 id。
我对 React/Flux 还是很陌生,不确定我做错了什么或为什么会这样。
主要组件删除功能:
deleteItem = (id) => {
console.log(id) //logs out 56b36c34ad9c927508c9d14f
QuestionStore.deleteQuestion(id);
}
此时id仍然只是id。
问题库:
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');
问题来源:
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
调用结果的一部分。所以所有参数都向右移动一个,你调用的第一个参数函数 with 依次成为第二个参数。请参见下面的代码示例:
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
}
}