react剛?cè)腴T,學(xué)習(xí)element-ui carousel源碼后亭敢,準(zhǔn)備做一個(gè)輪播圖培慌,在react16.8之后推薦使用hook編寫,現(xiàn)在父組件在調(diào)用子組件方法遇到了問(wèn)題篇裁,源碼使用class組件沛慢,我用函數(shù)組件,大概思意思是在CarouselItem 組件 componentWillMount時(shí)达布,向Carousel組件中items(Array)添加CarouselItem颠焦,之后再調(diào)用 CarouselItem組件中 translateItem方法;在class組件中 直接通過(guò)"."得方式就能訪問(wèn)到往枣,在函數(shù)組件中顯然是不行的(違反hook使用原則:不能再循環(huán)中使用伐庭,只能在函數(shù)組件的主體內(nèi)部調(diào)用);求助有沒(méi)有辦法或者其他好的方案
這是源碼中分冈,其中
```javascript
this.state.items=[CarouselItem,CarouselItem,CarouselItem...]
? _resetItemPosition(oldIndex: number): void {
? ? this.state.items.forEach((item, index) => {
? ? ? item.translateItem(index, this.state.activeIndex, oldIndex);
? ? });
? }
```