最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react-native 完整實現(xiàn)登錄功能的示例代碼

 更新時間:2017年09月11日 14:45:51   作者:mangues  
本篇文章主要介紹了react-native 完整實現(xiàn)登錄功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

react native實現(xiàn)登錄功能,包括ui的封裝、網(wǎng)絡請求的封裝、導航器的實現(xiàn)、點擊事件。

demo下載:react-native 完整實現(xiàn)登錄功能

后臺如果是springmvc實現(xiàn)的需要配置上如下代碼

 <!--加入multipart 的解析器,這個必須配置,一會在controller里抓取上傳文件時要用。否則會報錯。-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <property name="maxUploadSize" value="102400"></property>

    <property name="defaultEncoding" value="utf-8"/><!--屬性:編碼-->

  </bean>

1.實現(xiàn)的界面

這里寫圖片描述

2.完整目錄

這里寫圖片描述

3.主界面的代碼實現(xiàn)

import React, { Component } from 'react';
import {
 ToolbarAndroid,
 AppRegistry,
 StyleSheet,
 Text,
 View,
 Image,
 TextInput,
 TouchableOpacity
} from 'react-native';
import EditView from '../lib/EditView';
import LoginButton from '../lib/LoginButton';
import LoginSuccess from '../ui/LoginSuccess';
import NetUitl from '../lib/NetUtil';
export default class LoginActivity extends Component {
 constructor(props) {
  super(props);
  this.userName = "";
  this.password = "";
 }

 render() {
   return (

  <View style={LoginStyles.loginview}>
   <View  style={{flexDirection: 'row',height:100,marginTop:1,
    justifyContent: 'center',
    alignItems: 'flex-start',}}>
    <Image source={require('../image/login.png')}/>
   </View>
   <View style={{marginTop:80}}>
    <EditView name='輸入用戶名/注冊手機號' onChangeText={(text) => {
      this.userName = text;
    }}/>
    <EditView name='輸入密碼' onChangeText={(text) => {
      this.password = text;
    }}/>
    <LoginButton name='登錄' onPressCallback={this.onPressCallback}/>
    <Text style={{color:"#4A90E2",textAlign:'center',marginTop:10}} >忘記密碼?</Text>
   </View>
   </View>
  )
 }


 onPressCallback = () => {
  let formData = new FormData();
  formData.append("loginName",this.userName);
  formData.append("pwd",this.password);
  let url = "http://localhost:8080/loginApp";
  NetUitl.postJson(url,formData,(responseText) => {
     alert(responseText);
     this.onLoginSuccess();
  })


 };

 //跳轉(zhuǎn)到第二個頁面去
  onLoginSuccess(){
   const { navigator } = this.props;
   if (navigator) {
    navigator.push({
     name : 'LoginSuccess',
     component : LoginSuccess,
    });
   }
  }

}

class loginLineView extends Component {
 render() {
  return (
    <Text >
      沒有帳號
     </Text>
  );
 }
}

const LoginStyles = StyleSheet.create({
 loginview: {
  flex: 1,
  padding: 30,
   backgroundColor: '#ffffff',
 },
});

說明:

1.使用了線性布局,從上往下依次Image,EditView,LoginButton,Text

2.EditView和LoginButton 為自定義控件,實現(xiàn)輸入框,和按鈕的封裝。

4.EditView.js

import React, { Component } from 'react';
import {
 ToolbarAndroid,
 AppRegistry,
 StyleSheet,
 Text,
 View,
 Image,
 TextInput,
 TouchableOpacity
} from 'react-native';
export default class EditView extends Component {
 constructor(props) {
  super(props);
  this.state = {text: ''};
 }

 render() {
  return (
   <View style={LoginStyles.TextInputView}>
    <TextInput style={LoginStyles.TextInput}
     placeholder={this.props.name}
     onChangeText={
      (text) => {
       this.setState({text});
       this.props.onChangeText(text);
      }
    }
    />
    </View>
  );
 }
}


const LoginStyles = StyleSheet.create({
 TextInputView: {
  marginTop: 10,
  height:50,
  backgroundColor: '#ffffff',
  borderRadius:5,
  borderWidth:0.3,
  borderColor:'#000000',
  flexDirection: 'column',
  justifyContent: 'center',
 },

 TextInput: {
  backgroundColor: '#ffffff',
  height:45,
  margin:18,
 },
});

說明:

1.利用TextInput的onChangeText 方法獲取到輸入框中輸入的數(shù)據(jù),在利用EditView 傳入的onChangeText回調(diào)方法,把數(shù)據(jù)回調(diào)出封裝的EditView,在外部獲取到TextInput輸入的數(shù)據(jù)。

5.LoginButton.js

import React, { Component } from 'react';
import {
 ToolbarAndroid,
 AppRegistry,
 StyleSheet,
 Text,
 View,
 Image,
 TextInput,
 TouchableOpacity
} from 'react-native';
export default class LoginButton extends Component {
 constructor(props) {
  super(props);
  this.state = {text: ''};
 }
 render() {
  return (
   <TouchableOpacity onPress={this.props.onPressCallback} style={LoginStyles.loginTextView}>
    <Text style={LoginStyles.loginText} >
      {this.props.name}
    </Text>
   </TouchableOpacity>
  );
 }
}
const LoginStyles = StyleSheet.create({

 loginText: {
  color: '#ffffff',
   fontWeight: 'bold',
   width:30,
 },
 loginTextView: {
  marginTop: 10,
  height:50,
  backgroundColor: '#3281DD',
  borderRadius:5,
  flexDirection: 'row',
  justifyContent: 'center',
  alignItems:'center',
 },
});

說明:

1.利用TouchableOpacity包住Text實現(xiàn)點擊效果,onPress是點擊時調(diào)用,當點擊時onPress觸發(fā),調(diào)用外部傳入的onPressCallback 方法實現(xiàn)觸發(fā)事件在封裝的LoginButton外部定義觸發(fā)的效果。

6.NetUtil.js

let NetUtil = {
 postJson(url, data, callback){
    var fetchOptions = {
     method: 'POST',
     headers: {
      'Accept': 'application/json',
      'Content-Type': 'multipart/form-data;boundary=6ff46e0b6b5148d984f148b6542e5a5d'
     },
     body:data
    };

    fetch(url, fetchOptions)
    .then((response) => response.text())
    .then((responseText) => {
     // callback(JSON.parse(responseText));
      callback(responseText);
    }).done();
 },
}
export default NetUtil;

說明:網(wǎng)絡方法,依次傳入請求地址,請求參數(shù),成功回調(diào)事件

7.LoginSuccess.js

import React from 'react';
import {
  View,
  Navigator,
  TouchableOpacity,
  ToolbarAndroid,
  Text
} from 'react-native';
export default class LoginSuccess extends React.Component {
  constructor(props){
    super(props);
    this.state = {};

  }
  //回到第一個頁面去
  onJump(){
    const { navigator } = this.props;
    if (navigator) {
      navigator.pop();
    }
  }

  render(){
    return (

      <View >
        <TouchableOpacity onPress = {this.onJump.bind(this)}>
          <Text> 登錄成功,點擊返回登錄頁面 </Text>
        </TouchableOpacity>
      </View>


    );

  }

}

說明:登錄成功后跳轉(zhuǎn)的界面

8.navigator.js

導航器控制類。利用name,component 實現(xiàn)導航(可以自己隨便定義命名,只要后面的類中訪問同樣的命名即可,課參考LoginSuccess.js 中的返回功能)

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
 AppRegistry,
 StyleSheet,
 Text,
 View,
 Navigator
} from 'react-native';
import Main from './ui/main';
export default class navigator extends Component {
  constructor(props) {
   super(props);
  }
  render() {
  let defaultName = 'Main';
  let defaultComponent = Main;
  return (
   <Navigator
    initialRoute = {{name : defaultName , component: defaultComponent}}
    configureScene = {(route) => {
     return Navigator.SceneConfigs.VerticalDownSwipeJump;
    }}
    renderScene={(route,navigator) => {
      let Component = route.component;
      return <Component {...route.params} navigator = {navigator} />
    }}
    />
  );
 }

};

9.index.android.js

規(guī)定的類

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, { Component } from 'react';
import {
 ToolbarAndroid,
 AppRegistry,
 StyleSheet,
 Text,
 View,
 Image,
 TextInput,
 TouchableOpacity
} from 'react-native';
import Navigator from './app/navigator';
AppRegistry.registerComponent('AwesomeProject', () => Navigator);

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解React服務端渲染從入門到精通

    詳解React服務端渲染從入門到精通

    這篇文章主要介紹了詳解React服務端渲染從入門到精通,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 使用Webpack打包React項目的流程步驟

    使用Webpack打包React項目的流程步驟

    隨著React應用日益復雜,開發(fā)者需要借助模塊打包工具來管理項目依賴、轉(zhuǎn)換代碼和優(yōu)化性能,Webpack是一款功能強大的模塊打包器,它可以將React項目中的JavaScript、CSS、圖片等資源打包成瀏覽器友好的文件,本文將全面介紹如何使用Webpack打包React項目
    2025-03-03
  • React組件的生命周期深入理解分析

    React組件的生命周期深入理解分析

    組件的生命周期就是React的工作過程,就好比人有生老病死,自然界有日月更替,每個組件在網(wǎng)頁中也會有被創(chuàng)建、更新和刪除,如同有生命的機體一樣
    2022-12-12
  • 用React實現(xiàn)一個類 chatGPT 的交互式問答組件的方法詳解

    用React實現(xiàn)一個類 chatGPT 的交互式問答組件的方法詳解

    這篇文章主要給大家詳細介紹如何用React實現(xiàn)一個類 chatGPT 的交互式問答組件的方法,文中有詳細的代碼示例,對我們學習有一定的幫助,需要的朋友可以參考下
    2023-06-06
  • React父子組件間的傳值的方法

    React父子組件間的傳值的方法

    在單頁面里面,父子組件傳值是比較常見的,這篇文章主要介紹了React父子組件間的傳值的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • React合成事件原理及實現(xiàn)(React18和React16)

    React合成事件原理及實現(xiàn)(React18和React16)

    本文主要介紹了React合成事件原理及實現(xiàn),包含React18和React16兩種版本,具有一定的參考價值,感興趣的可以了解一下
    2025-02-02
  • React高階組件的使用淺析

    React高階組件的使用淺析

    高階組件就是接受一個組件作為參數(shù)并返回一個新組件(功能增強的組件)的函數(shù)。這里需要注意高階組件是一個函數(shù),并不是組件,這一點一定要注意,本文給大家分享React高階組件使用小結(jié),一起看看吧
    2022-08-08
  • React 組件通信的多種方式與最佳實踐

    React 組件通信的多種方式與最佳實踐

    在現(xiàn)代前端開發(fā)中,組件化是 React 的核心理念之一,組件之間的通信是構(gòu)建復雜應用的重要組成部分,在這篇文章中,我們將深入探討 React 組件通信的多種方式,包括它們的優(yōu)缺點、使用場景以及最佳實踐,需要的朋友可以參考下
    2024-11-11
  • ReactJS中的自定義組件實例代碼

    ReactJS中的自定義組件實例代碼

    React 是一個用于構(gòu)建用戶界面的 JAVASCRIPT 庫。這篇文章主要介紹了ReactJS中的自定義組件的代碼講解,需要的朋友可以參考下
    2019-11-11
  • React如何解決fetch跨域請求時session失效問題

    React如何解決fetch跨域請求時session失效問題

    這篇文章主要給大家介紹了關于React如何解決fetch跨域請求時session失效問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11

最新評論

梨树县| 金寨县| 宝山区| 泰安市| 永福县| 内乡县| 景德镇市| 共和县| 贡山| 色达县| 桑植县| 延津县| 大名县| 宣汉县| 亳州市| 娱乐| 堆龙德庆县| 五台县| 宜君县| 樟树市| 和硕县| 鹤山市| 浮山县| 惠州市| 武邑县| 卢氏县| 奎屯市| 蒙城县| 江津市| 图片| 太和县| 色达县| 孝义市| 肥西县| 祥云县| 乌拉特后旗| 区。| 白山市| 郯城县| 五华县| 三穗县|