[英]Passing refs for carousel into typescript using React and Ant design?
I'm trying to implement buttons to scroll a carousel using React + Ant Design.我正在尝试使用 React + Ant Design 实现按钮来滚动轮播。
I have managed to successfully implement it with a single carousel using React refs.我已经使用 React refs 成功地通过单个轮播实现了它。
I would like to make the button scroll two carousel instead.我想让按钮滚动两个轮播。 So i lifted the scrolling functions
nextPane
and prevPane
into a parent class called LandingPage
.所以我将滚动功能
nextPane
和prevPane
到名为LandingPage
的父 class 中。 Here I also constructed two React.createRef
for each of the carousel and pass them in into the corresponding components.这里我还为每个轮播构造了两个
React.createRef
并将它们传递到相应的组件中。
LandingPage.tsx登陆页面.tsx
class LandingPage extends Component<Props, State> {
// ToDo
private imageCarousel: any;
private textCarousel: any;
constructor(props: Props) {
super(props);
this.imageCarousel = React.createRef();
this.textCarousel = React.createRef();
}
nextPane = () => {
this.imageCarousel.next();
this.textCarousel.next();
};
prevPane = () => {
this.imageCarousel.prev();
this.textCarousel.prev();
};
render() {
return (
<Fragment>
<NavBar title="Athena."></NavBar>
<ImageCarouselWrapper>
<ImageCarousel
ref={this.imageCarousel}
nextPane={this.nextPane}
prevPane={this.prevPane}
/>
<WrapperCarouselOverlay>
<CarouselOverlay ref={this.textCarousel} />
</WrapperCarouselOverlay>
</ImageCarouselWrapper>
</Fragment>
);
}
}
ImageCarousel.tsx ImageCarousel.tsx
interface State {}
interface Props {
ref: any;
nextPane: () => void;
prevPane: () => void;
}
class ImageCarousel extends Component<Props, State> {
private carousel: any;
constructor(props: Props) {
super(props);
this.carousel = React.createRef();
}
render() {
const { nextPane, prevPane, ref } = this.props;
return (
<CarouselWrapper>
<RegularButtonWrapper>
<RegularButton size="large" icon="caret-left" onClick={prevPane} />
</RegularButtonWrapper>
<FlexCarousel>
<Carousel
ref={ref} // This doesn't work, neither does 'node => ({ref} = node)'
speed={700}
effect="scrollx"
>
</img>
</Carousel>
</FlexCarousel>
<RegularButtonWrapper>
<RegularButton size="large" icon="caret-right" onClick={nextPane} />
</RegularButtonWrapper>
</CarouselWrapper>
);
}
}
I am running into an error passing the ref whereby clicking any of the button yields:我在传递 ref 时遇到错误,单击任何按钮都会产生:
TypeError: this.imageCarousel.next is not a function
How do I pass and manipulate the refs properly into the two carousel component?如何将参考正确传递和操作到两个轮播组件中?
What is the type for React.createRef()
? React.createRef()
的类型是什么? I am using any
currently which I do not think is correct.我正在使用
any
我认为不正确的当前。
You need to forward the refs to the Carousel
components.您需要将 refs 转发到
Carousel
组件。 This is much simpler to do with functional components, as with class-based components you'll need to make a wrapper function to forward them for you like in the last example from the documentation, since class components do not receive ref props:这对于功能组件来说要简单得多,因为对于基于类的组件,您需要制作一个包装器 function 来为您转发它们,就像文档中的最后一个示例一样,因为 class 组件不接收参考道具:
class ImageCarousel extends Component<Props, State> {
...
}
const ForwardWrapper = (props, ref) => {
return <ImageCarousel {...props} ref={ref} />;
}
const CarouselWithRef = React.forwardRef(ForwardWrapper);
Then you can use it like a regular component:然后你可以像使用普通组件一样使用它:
<CarouselWithRef {...props} ref={this.imageCarousel}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.