PureComponent在React Native中是否与样式数组一起使用?

时间:2018-03-24 07:49:37

标签: reactjs react-native

我知道如果样式由Stylesheet.create()创建并且Child扩展PureComponent,则会优化以下代码段。

import React, { Component } from 'react'
import { View } from 'react-native'
import Child from './Child'
import styles from './stylesheets'

export default class Parent extends Component {

  render() {
    return (
      <View>
        <Child style={styles.x} />
      </View>
    )
  }
}

它是否也适用于多种样式?我担心[]文字会妨碍优化。

import React, { Component } from 'react'
import { View } from 'react-native'
import Child from './Child'
import styles from './stylesheets'

export default class Parent extends Component {

  render() {
    return (
      <View>
        <Child style={[styles.x, styles.y]} />
      </View>
    )
  }
}

或者我是否需要静态定义组合样式?

import React, { Component } from 'react'
import { View } from 'react-native'
import Child from './Child'
import styles from './stylesheets'

const childStyles = [styles.x, styles.y]

export default class Parent extends Component {

  render() {
    return (
      <View>
        <Child style={childStyles} />
      </View>
    )
  }
}

1 个答案:

答案 0 :(得分:0)

我确认数组文字会破坏PureComponent优化。