react-native-login-redux
项目地址如下
https://github.com/agunbuhori/react-native-login-redux
先看页面

```js
// 还有中英文切换
//src/global.js
import Theme from './styles/Theme';
import Auth from './auth/Auth';
import i18n from 'react-native-i18n';
import en from './localizations/en';
i18n.translations = {
en: en
}
export { Theme, Auth, i18n }
```js
//根app
//app.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from 'react';
import { Platform, StyleSheet } from 'react-native';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import rootReducer from './src/reducers/rootReducer';
//多语言切换
import { Auth } from './src/global';
const store = createStore(rootReducer);
import AuthNavigator from './src/navigators/AuthNavigator';
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
status: 0
}
}
componentDidMount() {
this.checkAuth();
this.checkReduxAuth();
}
async checkAuth() {
const authenticated = await Auth.checkAuth();
if (authenticated)
this.setState({status: 2});
else
this.setState({status: 1});
}
checkReduxAuth() {
store.subscribe(() => {
let authentication = store.getState().authentication;
if (authentication.authenticated)
this.setState({ status: 2 });
else
this.setState({ status: 1 });
});
}
// 根据renderApp进行判断
renderApp() {
switch (this.state.status) {
case 1:
return <AuthNavigator/>
break;
case 2:
return null;
break;
default:
return null;
}
}
render() {
return (
<Provider store={store}>
{this.renderApp()}
</Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
//login.js
import React, { Component } from 'react';
import { ActivityIndicator, KeyboardAvoidingView, AsyncStorage } from 'react-native';
import { connect } from 'react-redux';
import { StyleSheet } from 'react-native';
import { Form, Button, Text, View, Container, Item, Input, Icon } from 'native-base';
import { Theme, i18n, Auth } from '../../global';
import { loginSuccess } from '../../actions/authentication';
import Image from 'react-native-scalable-image';
class Login extends Component {
state = {
loginProgress: false,
username: null,
password: null,
authentication: {
authenticated: false
}
}
componentDidMount() {
}
async login() {
this.setState({loginProgress: true});
const login = await Auth.login({username: this.state.username, password: this.state.password});
if (login.token) {
Auth.setAuthToken(login.token, this.state.username, this.state.password);
this.props.onLogin({authenticated: true});
}
}
renderLogo() {
return (
<View style={Theme.mb2}>
<Image source={require('../../assets/images/logo.png')} width={150} />
<Image source={require('../../assets/images/akari.png')} width={130} />
</View>
);
}
renderForm() {
return (
<Form style={styles.loginForm}>
<Item regular style={styles.loginInput}>
<Icon type="AntDesign" name="user"/>
<Input placeholder={i18n.t('username')} autoCapitalize="none" onChangeText={username => this.setState({username})}/>
</Item>
<Item regular style={styles.loginInput}>
<Icon type="AntDesign" name="lock"/>
<Input placeholder={i18n.t('password')} secureTextEntry={true} onChangeText={password => this.setState({password})}/>
</Item>
<Button block style={styles.loginButton} onPress={this.login.bind(this)} activeOpacity={1}>
{
this.state.loginProgress
? <ActivityIndicator color="white"/>
: <Text style={Theme.lightBold}>{i18n.t('login')}</Text>
}
</Button>
</Form>
);
}
render() {
return (
<KeyboardAvoidingView behavior="padding" style={styles.container}>
{this.renderLogo()}
{this.renderForm()}
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
...Theme.p1,
...Theme.centered
},
loginForm: {
width: '100%'
},
loginInput: {
...Theme.bgSecondary,
...Theme.mb1,
...Theme.r1
},
loginButton: {
...Theme.bgPrimary,
...Theme.r1,
...Theme.primaryButton
}
});
const mapStateToProps = state => ({
authentication: state.authentication,
});
const mapDispatchToProps = dispatch => ({
onLogin: (user) => {
dispatch(loginSuccess(user));
},
});
export default connect(mapStateToProps, mapDispatchToProps)(Login);

咩有登陆进去,还是存在一些问题
react-native-login-redux的更多相关文章
- [RN] React Native 使用 Redux 比较详细和深刻的教程
React Native 使用 Redux 比较详细和深刻的教程 React Native 使用 Redux https://www.jianshu.com/p/06fc18cef56a http:/ ...
- React Native集成Redux框架讲解与应用
学过React Native的都知道,RN的UI是根据相应组件的state进行render的,而页面又是由大大小小的组件构成,导致每个组件都必须维护自身的一套状态,因此当页面复杂化的时候,管理stat ...
- react native 之 redux 使用套路
redux是什么?他是一个state容器 redux的运作方式是怎样的? 接入方式: 1. npm install 下列内容: npm install --save redux npm install ...
- react native 之 redux
第一章 认识redux 说的通俗且直白一点呢,就是redux提供了一个store,独立的一个内存区,然后放了一些state,你可以在任何component中访问到state,这些state要更改怎么 ...
- React Native使用Redux总结
1>npm安装redux: "react-redux": "^5.0.5", "redux": "^3.7.1", ...
- [转] 学习React Native必看的几个开源项目
http://www.lcode.org/study-react-native-opensource-one/ http://gold.xitu.io/entry/575f498c128fe10057 ...
- 学习React Native必看的几个开源项目
学习React native ,分享几个不错的开源项目,相信你学完之后,一定会有所收获.如果还没有了解RN的同学们可以参考手把手教你React Native 实战之开山篇<一> 1.Fac ...
- 从React Native到微服务,落地一个全栈解决方案
Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...
- React Native 开发豆瓣评分(三)集成 Redux
什么是 redux redux 是一个用于管理 js 应用状态(state)的容器.比如组件 A 发生了变化,组件 B 要同时做出响应.常见的应用场景就是用户的登录退出操作:未登录状态,个人中心显示登 ...
- react native redux saga增加日志功能
redux-logger地址:https://github.com/evgenyrodionov/redux-logger 目前Reac native项目中已经使用redux功能,异步中间件使用red ...
随机推荐
- Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
P2272 [ZJOI2007]最大半连通子图 题意 题目描述 一个有向图\(G=(V,E)\)称为半连通的\((Semi-Connected)\),如果满足:\(\forall u,v\in V\) ...
- Windows API 第六篇 GetLocalTime
GetLocalTime获取系统时间信息.函数原型:VOID WINAPI GetLocalTime( __out LPSYSTEMTIME lpSystemTime ); 先来看S ...
- CA证书制作
目录 手动制作CA证书 1.安装 CFSSL 2.初始化cfssl 3.创建用来生成 CA 文件的 JSON 配置文件 4.创建用来生成 CA 证书签名请求(CSR)的 JSON 配置文件 5.生成C ...
- PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么)
PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么) 一.总结 一句话总结: 拿时间换空间:针对内存泄露的情况,可以节省大量的内存空间,但是由于垃圾回收算法运行耗费时间,开启垃圾回收算法会增加脚本 ...
- 深入浅出 Java Concurrency (18): 并发容器 part 3 ConcurrentMap (3)[转]
在上一篇中介绍了HashMap的原理,这一节是ConcurrentMap的最后一节,所以会完整的介绍ConcurrentHashMap的实现. ConcurrentHashMap原理 在读写锁章节部分 ...
- 写一个网页进度loading
作者:jack_lo www.jianshu.com/p/4c93f5bd9861 如有好文章投稿,请点击 → 这里了解详情 loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在 ...
- PAT甲级——A1065 A+B and C (64bit)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...
- 用Jmeter参数化实现接口自动化测试
本文记录如何使用Jmeter参数化(csv)实现接口自动化——测试Token不同入参情况下,接口请求能够返回正确的结果 1. 首先需要使用Jmeter获取一个Token,如何获取暂略(同一般访问请求方 ...
- ajax返回后台编译时都对,返回error
首先看看你的dataType:‘json’ 类型是否与后台获取的类型一直.特别是json的格式对不对. 第二: 红括号里的有没有加
- 垂直对齐:vertical-align属性——使用中注意事项
1.vertical-align(垂直对齐),只对行内元素和单元格元素有效,例如属性为inline和inline-block的元素以及图片.输入表单等都是行内元素; 2.元素默认的垂直对齐方式为基线对 ...