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 ...
随机推荐
- [NOI2015] 软件包管理器【树链剖分+线段树区间覆盖】
Online Judge:Luogu-P2146 Label:树链剖分,线段树区间覆盖 题目大意 \(n\)个软件包(编号0~n-1),他们之间的依赖关系用一棵含\(n-1\)条边的树来描述.一共两种 ...
- PKUWC&SC 2018 刷题记录
PKUWC&SC 2018 刷题记录 minimax 线段树合并的题,似乎并不依赖于二叉树. 之前写的草率的题解在这里:PKUWC2018 minimax Slay the Spire 注意到 ...
- Codeigniter 数据库操作事务情况下获取不到last_insert_id()
开发中,数据库Insert使用了事务,如果 $this->db->insert_id() 放在 $this->db->trans_complete(); 这句语句之后,$thi ...
- Python中的sort()
Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可 ...
- Twisted的WEB开发
1 简介 在WEB开发中,偶尔需要对HTTP协议更多底层细节进行控制,这时的django/web.py等等显然无法满足要求,所以只好求助于Twisted了.使用Twisted进行WEB开发,其实更 ...
- HZOI2019 A. 那一天我们许下约定 dp
题目大意:https://www.cnblogs.com/Juve/articles/11219089.html 读这道题的题目让我想起了... woc我到底在想什么?好好写题解,现在不是干那个的时候 ...
- select有条件in要按照in中的数据排序
mybatis中的写法 <select id="selectByIds" resultType="com.hoohui.electronic.util.ExHash ...
- Java文件写入
一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函 ...
- 使用ssh时报错:Service对象空指针异常
有可能是spring容器不能自动生成service对象,导致空指针异常,常见的情况可能是在service前面加@Service注释
- 一、初识asyncio协程
初识asyncio协程 一.基本概念 要想了解学习协程相关知识要先对以下几个概念先行了解: 阻塞 阻塞状态是指程序未得到某所需计算资源时的挂起状态,简单说就是程序在等待某个操作未执行完前无法执 ...