createClass到Component类React Conversion

时间:2017-04-26 23:26:33

标签: javascript reactjs react-native

我有这种格式的代码:

  const Test = React.createClass({
      tabIcons: [],
      propTypes: {
      ...
      },
      render() {
       return
       <View> ...</View>
     }
  })
  

我不确定如何将tabIcons转换为新格式

class Test extends Component {
 static propTypes = {
  ...
  }
 render () {
  return (
   <View> ...</View>
  )
 }
}

1 个答案:

答案 0 :(得分:0)

您可以在构造函数中添加tabIcons作为类属性。

class Test extends Component {

 constructor(){
  this.tabIcons = [];
 }

 static propTypes = {
  ...
 }

 render () {
  return (
   <View> ...</View>
  )
 }
}

现在,您可以在类似州的任何地方访问它。

this.tabIcons.push(newItem)