Skip to content

Commit 444421c

Browse files
committed
cancel state
1 parent 15eea14 commit 444421c

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

react-swipeable/src/App.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import React, {Component} from "react";
22
import CustomAutoPlay from "./CustomAutoPlay";
3+
import mod from "react-swipeable-views/lib/utils/mod";
4+
35

46
class App extends Component {
5-
render() {
6-
let children = [
7+
8+
constructor(props) {
9+
super(props);
10+
this.state = {index: 0};
11+
}
12+
13+
static defaultProps = {
14+
children: [
715
<div id="test">
816
{'slide n°0'}
917
</div>,
@@ -16,27 +24,48 @@ class App extends Component {
1624
<div id="test">
1725
{'slide n°3'}
1826
</div>,
19-
];
27+
]
28+
};
2029

21-
return(
30+
render() {
31+
let {children} = this.props;
32+
let {index}=this.state;
33+
return (
2234
<div>
2335
<CustomAutoPlay
2436
ref="customAutoplay"
2537
children={children}
2638
autoplay={false}
2739
interval={3500}
28-
changeIndexCallBack={(index)=>{
29-
console.debug(index);
40+
index={index}
41+
changeIndexCallBack={(index)=> {
42+
this.setIndex(index);
43+
console.info("changing index"+index);
3044
}}
3145
/>
32-
<div className="button" onClick={this._click}>Click Me</div>
46+
<div className="button" onClick={this._click.bind(this, +1)}>Click Me++++</div>
47+
<div className="button" onClick={this._click.bind(this, +1)}>{"index=" + index}</div>
48+
<div className="button" onClick={this._click.bind(this, -1)}>Click Me----</div>
49+
3350
</div>
3451
);
3552
}
3653

37-
_click=()=>{
38-
this.refs.customAutoplay.changeIndex(2)
54+
setIndex = (index)=> {
55+
this.setState({index})
56+
}
57+
58+
_click = (number)=> {
59+
let {children} = this.props;
60+
let childrenLength = children.length;
61+
62+
let index = mod(this.state.index + number, childrenLength)
63+
this.setState({
64+
index: index
65+
})
3966
}
67+
68+
4069
}
4170

4271
export default App;

react-swipeable/src/CustomAutoPlay.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class CustomAutoPlay extends Component {
1717

1818
constructor(props) {
1919
super(props);
20-
this.state = {index: 0};
2120
}
2221

2322
static defaultProps = {
@@ -27,6 +26,7 @@ class CustomAutoPlay extends Component {
2726
};
2827

2928
shouldComponentUpdate(nextProps, nextState) {
29+
console.log('begin compare');
3030
return shallowCompare(this, nextProps, nextState);
3131
}
3232

@@ -72,15 +72,15 @@ class CustomAutoPlay extends Component {
7272
}
7373

7474
handleInterval = () => {
75-
let indexLatest = this.state.index;
75+
let indexLatest = this.props.index;
7676
let {children}=this.props;
7777
let indexNew = mod((indexLatest + 1), children.length);
7878
this.changeIndex(indexNew)
7979
};
8080

8181

8282
onChangeIndex = (index, indexLatest)=> {
83-
let stateIndex = this.state.index;
83+
let stateIndex = this.props.index;
8484
if ((index - indexLatest) > 0) {
8585
stateIndex += 1;
8686
} else if ((index - indexLatest) < 0) {
@@ -102,18 +102,15 @@ class CustomAutoPlay extends Component {
102102
changeIndex = (index)=> {
103103
let {children}=this.props;
104104
let childrenLength = children.length;
105-
index = mod(index, childrenLength)
106-
this.setState({
107-
index: index
108-
})
105+
index = mod(index, childrenLength);
109106
let {changeIndexCallBack}=this.props;
110107
changeIndexCallBack && changeIndexCallBack(index)
111108

112109
}
113110

114111

115112
render() {
116-
let {index}=this.state;
113+
let {index}=this.props;
117114
return (
118115
<EnhancedSwipeableViews
119116
index={index}

0 commit comments

Comments
 (0)