我是新来对付js的。我创建了一个简单的滑块组件。它正在工作,但我想问问是否有正确的方法来调用该方法?至于我在构造函数中调用它的那一刻。我真的很感激任何建设性的批评。谢谢。在哪里调用反应组件中的方法?
import React from 'react';
export default class Slider extends React.Component {
constructor() {
super();
this.state = {
current: 4
};
this.current = this.state.current;
setTimeout(() => {
this.animate();
}, 5000);
}
animate() {
this.getCurrent();
this.setState({
current: this.current
});
}
getCurrent() {
this.current = (this.current >= this.props.slides.length - 1) ? 0 : this.current + 1;
return this.current;
}
render() {
var slides = this.props.slides;
var slideList = slides.map((slide, index) => {
let slideClass = (index == this.state.current) ? 'slider-item active' : 'slider-item';
return <div className={slideClass} key={index}><span>{slide}</span></div>;
});
return (
<div className="slider-component">{slideList}</div>
)
}
}
我认为更好的选择将使用'componentDidMount'函数。 –