javascript - In a React.js app, an API calls fails in the store, how do I transmit that back to the view? -
https://cask.scotch.io/2014/10/v70csec.png
^ according flow, have make change in store, picked view.
more specifically, trying delete user, when store gets error db, want show modal saying error occurred. right way of transmitting message done through store variables , picked in view on getstatefromflux method?
userstore = { initialize: function(options) { // other variables this.userdeletionerror = false; }, deleteuser: function(payload) { axios.delete(dburl) .then((response) => { // succeeds }) .catch((error) => { // other error handling this.userdeletionerror = true; }); } }
if understand using ajax call in store, antipattern. right way make call in action file transmit in store.
to answer question flux-pattern should (not sure if match 100% use case).
- view => user want delete 'user', clicks on delete button
- view triggers action.
- action triggers ajax call 'delete user'
action receives answer , transmit store (here using react dispatcher , example below:
myajaxcall.then(function(answer) { dispatcher.handleviewaction({ actiontype: constants.actiontypes.delete_user, result: answer }); });
5.your store catching ajax answer still through dispatcher (example below:
mystore.dispatcherindex = dispatcher.register(function(payload) { var action = payload.action; var result; switch(action.actiontype) { case constants.actiontypes.user_delete: registeranswer(action.result); mystore.emitchange(); break; } return true; });
you can see store trigger registeranswer(), in function can check if ajaxcall has been executed (i mean user deleted or not) , accordingly build object. here there 2 way tell view answer status 1. build dataanswer object field message example , view can check 2. emit special event. prefer first way if find more generic.
store emitchange , view catch event (example below:
componentdidmount: function() { mystore.addchangelistener(address, this._ondeleteuser; },
then view check 'message' field filled in store accordingly answer , can render whatever appropriate.
i hope it's clear. here example of store in case need it. https://facebook.github.io/flux/docs/todo-list.html#creating-stores
to resume, approach except doing ajax call in store. don't it's bad. hope helps
Hi admin,
ReplyDeleteThanks for sharing this reactjs steps. It is really helpful.
React js Training in Chennai | React js Training
This blog is unique from all others. Thanks for sharing this content in an excellent way. Waiting for more updates.
ReplyDeleteSpoken English Class in Anna Nagar
Spoken English Classes in Chennai Anna Nagar
Spoken English Classes in Anna Nagar West
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
English Classes in Chennai
Thank you for uploading such a wonderful content on recent technology.
ReplyDeleteHadoop Training in Chennai
Big data training in chennai
Big Data Course in Chennai
JAVA Training in Chennai
Python Training in Chennai
Selenium Training in Chennai
Hadoop training in chennai
Big data training in chennai
big data course in chennai