import * as React from 'react';
import styles from './Birthday.module.scss';
import { IBirthdayProps } from './IBirthdayProps';
import { Icon } from 'office-ui-fabric-react/lib/Icon';
import { initializeIcons } from '@uifabric/icons';
initializeIcons();
import Slider from 'react-slick';
export default class Birthday extends React.Component<IBirthdayProps, {}> {
public next() {
this.slider.slickNext();
}
public previous() {
this.slider.slickPrev();
}
public render(): React.ReactElement<IBirthdayProps> {
const settings = {
dots: true,
infinite: true,
rows: 2,
slidesPerRow: 2,
slidesToShow: 1,
speed: 500,
appendDots: dots => (
<div
style={{
backgroundColor: '#ddd',
padding: '5px',
display: 'flex'
}}
>
<ul style={{ margin: '0px', padding: '0px' }}> {dots} </ul>
</div>
)
};
return (
<div className={styles.wrapper}>
<Slider ref={slider => (this.slider = slider)} {...settings}>
<div className={styles.item}>
<h3>1</h3>
</div>
<div className={styles.item}>
<h3>2</h3>
</div>
<div className={styles.item}>
<h3>3</h3>
</div>
<div className={styles.item}>
<h3>4</h3>
</div>
<div className={styles.item}>
<h3>5</h3>
</div>
</Slider>
<div style={{ display: 'flex', padding: '30px', marginTop: '30px' }}>
<Icon
iconName="ChevronLeftSmall"
className={styles.customArrow}
onClick={this.next}
/>
<Icon
iconName="ChevronRightSmall"
className={styles.customArrow}
onClick={this.previous}
/>
</div>
</div>
);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
我正在尝试使用反应灵敏的旋转木马。 我尝试对其进行一些自定义,所以我创建了下一个和上一个按钮,该按钮应触发下一个和上一个轮播的方法。 明确地说,我正在使用SPFX开发,这是SharePoint框架,React with Typescript。 编写代码时出现以下错误:(与Slider Component中的this.slider相同)
我由他们的文档在这里开发: https://react-slick.neostack.com/docs/example/previous-next-methods 我尝试过将变量创建为Slide和Slider,然后将其添加为props或state或仅添加到类中的变量,但没有成功。
我希望按钮触发下一个和上一个方法...
感谢任何帮助!
答案 0 :(得分:2)
使用功能组件
import React, { useState } from 'react'
import Slider from "react-slick";
const Homepage = () => {
//creating the ref
const customeSlider = React.creatRef();
// setting slider configurations
const [sliderSettings, setSliderSettings] = useState({
infinite: true,
speed: 500,
slidesToShow: 2,
slidesToScroll: 2,
arrows: false,
})
const gotoNext = () => {
customeSlider.current.slickNext()
}
const gotoPrev = () => {
customeSlider.current.slickPrev()
}
return (
<div>
<button onClick={()=>gotoNext()}>Next</button>
<button onClick={()=>gotoPrev()}>Previous</button>
<Slider {...sliderSettings} ref={customeSlider}>
<div>One</div>
<div>Two</div>
<div>Theree</div>
<div>Four</div>
<div>Five</div>
</Slider>
</div>
)
}
export default Homepage```
答案 1 :(得分:1)
示例演示:
import * as React from 'react';
import styles from './ReactSpFx.module.scss';
import { IReactSpFxProps } from './IReactSpFxProps';
import { escape } from '@microsoft/sp-lodash-subset';
import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration } from '@microsoft/sp-http';
import Slider from "react-slick";
import "../../../../node_modules/slick-carousel/slick/slick.css";
import "../../../../node_modules/slick-carousel/slick/slick-theme.css";
import { any } from 'prop-types';
export interface IReactItem{
ID:string,
Title:string,
Address:string
}
export interface IReactGetItemsState{
items:IReactItem[]
}
export default class ReactSpFx extends React.Component<IReactSpFxProps,IReactGetItemsState> {
public constructor(props: IReactSpFxProps) {
super(props);
this.state = {
items:[]
};
this.next = this.next.bind(this);
this.previous = this.previous.bind(this);
}
public componentDidMount() {
var reactHandler = this;
this.props.context.spHttpClient.get(`${this.props.context.pageContext.web.absoluteUrl}/_api/web/lists/getbytitle('TestList')/items?select=ID,Title,Address`,
SPHttpClient.configurations.v1) .then((response: SPHttpClientResponse) => {
response.json().then((responseJSON: any) => {
reactHandler.setState({
items: responseJSON.value
});
});
});
}
protected slider;
next() {
this.slider.slickNext();
}
previous() {
this.slider.slickPrev();
}
public render(): React.ReactElement<IReactSpFxProps> {
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1
};
return (
<div className={styles.reactSpFx}>
<div className={styles.container}>
{(this.state.items || []).map(item => (
<div key={item.ID} className={styles.row}>{item.Title}
<div dangerouslySetInnerHTML={{ __html: item.Address.replace(/[\n\r]/g,"<br/>")}}></div>
</div>
))}
</div>
<div>
<h2> Single Item</h2>
<Slider ref={c => (this.slider = c)} {...settings}>
<div>
<h3>1</h3>
</div>
<div>
<h3>2</h3>
</div>
<div>
<h3>3</h3>
</div>
<div>
<h3>4</h3>
</div>
<div>
<h3>5</h3>
</div>
<div>
<h3>6</h3>
</div>
</Slider>
<br/>
<div style={{ textAlign: "center" }}>
<button className="button" onClick={this.previous}>
Previous
</button>
<button className="button" onClick={this.next}>
Next
</button>
</div>
</div>
</div>
);
}
}
假设您的意思是IReactSpFxProps
import { WebPartContext } from '@microsoft/sp-webpart-base';
export interface IReactSpFxProps {
description: string,
context: WebPartContext;
}
答案 2 :(得分:0)
您需要为ref
定义类型,该类型必须为HTMLDivElement
的类型,在这里如何操作:
在您的班级组件中添加以下内容:
private slider = React.createRef<HTMLDivElement>()