I use Reflux for this purpose ( https://github.com/reflux/refluxjs ) in both React and React Native.
, , , .. , - ( ajax, ..), . ( - "" ).
import Reflux from 'reflux'
import LoginActions from '../actions/LoginActions'
var LoginStore = Reflux.createStore({
isLoggedIn: false,
profile: {},
init: function() {
},
listenables: [LoginActions],
loginUser: function(e) {
if (fetch_result = true) {
this.isLoggedIn = true;
this.trigger({type: "LOGIN_STATUS", loggedIn: true});
}
},
logoutUser: function(e) {
}
LoginActions , :
import Reflux from 'reflux'
export default LoginActions = Reflux.createActions([
'loginUser',
'logoutUser',
]);
, ( ), ; .
componentDidMount() {
this.login = LoginStore.listen(this.loginStoreListener.bind(this));
}
componenWillUnmount() {
this.login();
}
loginStoreListener(obj) {
switch (obj.type) {
case "LOGIN_STATUS":
this.setState({loggedIn: obj.loggedIn})
break;
case "PROFILE":
this.setState({profile: obj.profile})
break;
}
}
, loggedIn, , , , Navigator.