美文网首页程序员
React native之路(四)Props

React native之路(四)Props

作者: Knight_Davion | 来源:发表于2017-04-05 10:02 被阅读223次

Props官网的介绍是:

Most components can be customized when they are created, with different parameters. These creation parameters are called props.

意思是:组件可以在创建时使用不同的参数j进行自定义,这些参数就是Props。说白了就是java中的类属性啊(其实java中属性都应该会有一个set和get方法的,否则只能说这是一个成员变量)。
官网给我们举了一个Image source的例子:

import React, { Component } from 'react';
import { AppRegistry, Image } from 'react-native';

class Bananas extends Component {
  render() {
    return (
      <Image source={{ uri: 'https://upload.wikimedia.org/wikipedia/commons/d/de/Bananavarieties.jpg'}} style={{width: 193, height: 110}}/>
    );
  }
}

AppRegistry.registerComponent('Bananas', () => Bananas);

source就是Image 这个组件的一个属性,同理 style也是。
再来看一个button的例子,一个button有以下几个属性:



举个栗子

render(){
  return(
    <Button
      onPress={onButtonPress}
      title="Press Me"
      accessibilityLabel="See an informative alert"
    />
  );
  }

到这里我们应该基本了解了什么是Props吧,在以后实际开发过程中,可以通过API了解各个组件的属性。

以上说的都是已有组件的属性那么我们自定义组件时该如何设置属性呢?官网给的解决办法是:在自定义组件的渲染函数(render)中通过this.props定义你的属性即可。
还是来看官网的例子(略微修改了一下)

class Greeting extends Component {
  render() {
    return (
      <Text>{this.props.name}</Text>
    );
  }
}

我们在Greeting 这个自定义组件的渲染函数中通过<Text>组件定义了一个名为name的属性,注意看name前边的this.props,同时注意this.props.name是被{}包裹起来的。
如何使用

class LotsOfGreetings extends Component {
  render() {
    return (
      <View >
           <Greeting name='Android'></Greeting>
           <Greeting name='Ios'></Greeting>
           <Greeting name='WindowPhone'></Greeting>
      </View>
    );
  }
}

运行结果:

修改一下Greeting的属性

class Greeting extends Component {
  render() {
    return (
      <View>
        <Text>{this.props.name}</Text>
        <Text>{this.props.subname}</Text>
      </View>
    );
  }
}

class LotsOfGreetings extends Component {
  render() {
    return (
      <View >
           <Greeting name='Android' subname='Ios'></Greeting>
      </View>
    );
  }
}

实现了同样的效果,所以说属性的使用方式还是很灵活的官网其实也说了,实际开发过程中可能需要自定义各种各样的组件,合理使用好组件的属性,从而达到想要的效果。

相关文章

  • React native之路(四)Props

    Props官网的介绍是: Most components can be customized when they ...

  • React native props

    Image Style Props borderTopRightRadius backfaceVisibility...

  • React Native - Props

    Props 大多数组件在被创建的时候就能被自定义,带不同的参数,这些创造参数被称为props。 例如,一个基本的R...

  • react native学习笔记6——Props和State

    Props(属性)和State(状态)是React Native中很重要的两个概念。使用Props和State,结...

  • React-Native征程(2)

    http://facebook.github.io/react-native/docs/props.html官方文...

  • React Native

    React Native之路(一)Windows下安装配置 - 简书 react native学习笔记0——win...

  • React Native几个重要的属性

    React Native生命周期 Props 属性传递,单向。每一个组件都有可变与不可变的属性,props就是不可...

  • React Native - React Props(属性)

    Props(属性) 组件创建的时候需要用不同的参数进行定制,这些定制的参数就是props(属性),可为组件内部属性...

  • RN:Props

    React-Native:Props props:属性,大多数的组件在创建时可以使用各种参数进行设置,就是属性pr...

  • React Native的props

    前言 在React的世界里,界面是由一个个Component拼出来的。当我们需要渲染一个界面时,以为父...

网友评论

    本文标题:React native之路(四)Props

    本文链接:https://www.haomeiwen.com/subject/kmegottx.html