2017-04-26 38 views
1

我有这样的代码格式如下:createClass到组件类阵营转换

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

我不知道如何将tabIcons转换成新的格式

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

你可以做'tabIcons = []'像你proptypes – Li357

回答

0

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

class Test extends Component { 

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

static propTypes = { 
    ... 
} 

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

现在你可以在类似于状态的任何地方访问它。

this.tabIcons.push(newItem) 
+0

做我得到的错误'undefined是不是(评估“this.tabIcons.forEach”)'当我尝试把这个'一个对象。 tabIcons.forEach((icon,i)=> {})' – jasan

+0

你叫什么名字?也许你没有用'bind'正确设置上下文。 –

+0

看看这个要点https://gist.github.com/jasan-s/64a47c3d237748b5a1b5171c9fed4b6f – jasan