react-native-login-redux
2024-09-02 17:43:42
项目地址如下
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);
咩有登陆进去,还是存在一些问题
最新文章
- 与你相遇好幸运,Tippecanoe用法
- elk实战分析nginx日志文档
- 设置DIV隐藏与显示,表格滑动条
- (总结)CentOS Linux下VNC Server远程桌面配置详解
- ACCSESS数据库导入到SQL SEVERES2005
- idea新建项目文件名为红色的解决办法
- Vasiliy&#39;s Multiset
- (转)Spring boot——logback.xml 配置详解(三)<;appender>;
- java new 关键字到底做了什么?
- BT雷人的程序语言
- WordCount结对项目
- mac os ssh远程链接centos提示证书错误解决方法
- Oracle 迁移 序列
- 音视频编解码: YUV存储格式中的YUV420P,YUV420SP,NV12, NV21理解(转)
- 树形控件(CTreeCtrl和CTreeView)
- flask_maple使用文档
- OAuth的机制原理讲解及开发流程(转)
- tcp_tw_recycle和tcp_timestamps导致connect失败问题
- 无废话网页重构系列——(2)来套Web重构装备
- PHP与Java集成开发详解(一)
热门文章
- 语义分割--全卷积网络FCN详解
- object and namespace
- [JZOJ5969] 世界线修理(欧拉回路)
- Django项目:CRM(客户关系管理系统)--61--51PerfectCRM实现CRM客户报名流程学生合同上传照片
- 深入了解组件- -- Prop
- vim中利用swp文件进行恢复
- V8:V8(Javascript引擎)
- Multi-statement transaction required more than &#39;max_binlog_cache_size&#39; bytes of storage; increase this mysqld variable and try again
- springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
- 【Java爬虫】爬取南通大学教务处成绩