search
HomeWeb Front-endHTML Tutorial【REACT NATIVE 系列教程之三】函数绑定与FlexBox是用好React的基础_html/css_WEB-ITnose

Himi在写React 时主要遇到两个知识点觉得很有必要跟大家一起回顾下。

  1. 函数绑定
  2. FlexBox 布局

一:函数绑定

首先来看一段代码片段:

 constructor(props) {      super(props);      this.state = {        myName:'I am MyName!',      };  }  componentWillMount() {    this.state.myName='cwm';  }    testFun1(){    this.state.myName='tf1';    Alert.alert('Himi', ' testFun1 ');      }  testFun2(){    Alert.alert('Himi', ' testFun2 ');  }

在state中声明了myName

constructor: 组件的构造函数

componentWillMount : 组件预加载前调用的生命周期函数

testFun1、 testFun2 :是两个自定义的函数。

继续看render中的一段:

<TouchableHighlight          underlayColor='#4169e1'          onPress={this.testFun1}            >             <Image             source={require('./res/himi.png')}             style={{width: 70, height: 70}}             />        </TouchableHighlight>         <TouchableHighlight          underlayColor='#4169e1'          onPress={this.testFun2}            >             <Image             source={require('./res/himi.png')}             style={{width: 70, height: 70}}             />        </TouchableHighlight>

这里创建了两个图片组件且都添加了触摸组件,分别绑定自定义的函数testFun1 与 testFun2

当我们点击第一个图片时会报错,运行效果如下: (点击查看动态图)

错误是说this没有undefined,原因是因为当想在自定义的函数中使用this,那么需要进行函数绑定。

函数绑定: 函数进行 bind(绑定) 可以确保在函数中的 this 作为组件实例的引用,也就是说你想在自定义的函数中使用this,那么请先进行将此函数bind(this)

那么细心的童鞋会发现!为什么在 componentWillMount 函数中也使用了this却通过了?因为 componentWillMount 是组件的生命周期函数 。

那么常用的函数绑定方式有如下几种:

1.  在生命周期函数中绑定,如下:

this.testFun1 = this.testFun1.bind(this)

2. 使用的地方直接绑定,如下:

onPress={this.testFun1.bind(this)}

3. 直接在使用的地方直接lambda,更方便 ��  如下:

onPress={()=>{    this.state.myName='tf1';    Alert.alert('Himi', ' testFun1 ');}}

一:FlexBox 布局

关于CSS 的FlexBox 本篇不重新赘述了,一来是因为网上一搜一大把的教程,二来不一定有别人写的仔细 – -… 。但是,Himi这里推荐两个链接,学习足以:

1. 详细介绍与分析:   http://www.tuicool.com/articles/vQn6ZrU

2. 直观的教程: http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools