2017-04-26 64 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