Home  >  Article  >  Web Front-end  >  Getting Started with React Native

Getting Started with React Native

零下一度
零下一度Original
2017-06-26 10:53:031175browse

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##
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', () => LotsOfGreetings);
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##
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(() => {
      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', () => BlinkApp);
Getting Started with React Native
Example
2:

Getting Started with React Native
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})}
        />
        <text>
          {this.state.text.split(' ').map((word) => word && '</text></textinput></view>

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:
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