Home Web Front-end JS Tutorial Getting Started with React Native

Getting Started with React Native

Jun 26, 2017 am 10:53 AM
native react how understand

Most people who are new to react native are deeply confused about this, so I will once again share an introductory article about react native

1: Props (property)

Most components can be customized using various parameters when they are created. These parameters used for customization are called props (properties). props are specified in the parent component, and once specified, they will not change during the life cycle of the specified component

Customized by using different properties in different scenarios , you can maximize the reuse scope of custom components. Just reference this.props in the render function and handle it as needed. Here is an example:

Getting Started with React Native##

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import React, { Component } from 'react';

import { AppRegistry, Text, View } from 'react-native';

 

class Greeting extends Component {

  render() {

    return (

      <text>Hello {this.props.name}!</text>

    );

  }

}

 

class LotsOfGreetings extends Component {

  render() {

    return (

      <view>

        <greeting></greeting>

        <greeting></greeting>

        <greeting></greeting>

      </view>

    );

  }

}

 

AppRegistry.registerComponent('LotsOfGreetings', () =&gt; LotsOfGreetings);

Copy after login
Getting Started with React Native

2: State (state)

We use two kinds of data to control a component:

props and state. Props are specified in the parent component, and once specified, they will not change during the life cycle of the specified component. For data that needs to be changed, we need to use state.

Generally speaking, you need to initialize the

state in the constructor (Annotation: This is how ES6 is written, Many early ES5 examples used the getInitialState method to initialize the state, which will gradually be eliminated), and then call the setState method when modifications are needed.

Suppose we need to create a text that keeps flashing. The text content itself has been specified when the component is created, so the text content should be a

prop. The display or hidden state of text (fast switching between display and concealment produces a flickering effect) changes over time, so this state should be written in state.

Getting Started with React Native##

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

import React, { Component } from 'react';

import { AppRegistry, Text, View } from 'react-native';

 

class Blink extends Component {

  constructor(props) {

    super(props);

    this.state = { showText: true };

 

    // 每1000毫秒对showText状态做一次取反操作

    setInterval(() =&gt; {

      this.setState({ showText: !this.state.showText });

    }, 1000);

  }

 

  render() {

    // 根据当前showText的值决定是否显示text内容

    let display = this.state.showText ? this.props.text : ' ';

    return (

      <text>{display}</text>

    );

  }

}

 

class BlinkApp extends Component {

  render() {

    return (

      <view>

        <blink></blink>

        <blink></blink>

        <blink></blink>

        <blink></blink>

      </view>

    );

  }

}

 

AppRegistry.registerComponent('BlinkApp', () =&gt; BlinkApp);

Copy after login
Getting Started with React Native
Example
2:

Getting Started with React Native

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import React, { Component } from 'react';

import { AppRegistry, Text, TextInput, View } from 'react-native';

 

class PizzaTranslator extends Component {

  constructor(props) {

    super(props);

    this.state = {text: ''};

  }

 

  render() {

    return (

      <view>

        <textinput> this.setState({text})}

        /&gt;

        <text>

          {this.state.text.split(' ').map((word) =&gt; word &amp;&amp; '</text></textinput></view>

Copy after login

The above is the detailed content of Getting Started with React Native. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build a real-time chat app with React and WebSocket How to build a real-time chat app with React and WebSocket Sep 26, 2023 pm 07:46 PM

How to build a real-time chat app with React and WebSocket

Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end Sep 28, 2023 am 10:48 AM

Guide to React front-end and back-end separation: How to achieve decoupling and independent deployment of front-end and back-end

How to build simple and easy-to-use web applications with React and Flask How to build simple and easy-to-use web applications with React and Flask Sep 27, 2023 am 11:09 AM

How to build simple and easy-to-use web applications with React and Flask

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging app with React and RabbitMQ

How to build a fast data analysis application using React and Google BigQuery How to build a fast data analysis application using React and Google BigQuery Sep 26, 2023 pm 06:12 PM

How to build a fast data analysis application using React and Google BigQuery

React code debugging guide: How to quickly locate and solve front-end bugs React code debugging guide: How to quickly locate and solve front-end bugs Sep 26, 2023 pm 02:25 PM

React code debugging guide: How to quickly locate and solve front-end bugs

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

React Router User Guide: How to implement front-end routing control

How to package and deploy front-end applications using React and Docker How to package and deploy front-end applications using React and Docker Sep 26, 2023 pm 03:14 PM

How to package and deploy front-end applications using React and Docker

See all articles