selectedItem retains control when an `onChange()` sets it manually
<Carousel autoPlay={true}
interval={10000}
showThumbs={false}
infiniteLoop={true}
showStatus={false}
onChange={()=>this.setState({changed:!this.state.changed})}
dynamicHeight={true}
>
Yes, you can. The index of the selected item will be passed as argument.
sorry I don't understand it doesn't work as I expected when I set the state can you explain more please?
What happens when you do it?
the autoplay doesn't work and it only display the first two slides when I change them manually
I need to see an example to be able to help. However, I won't be close to a computer for the next 2 weeks so there is not much I can do other than maybe point you to the right thing. Sorry! If you find the problem in the code and wish to submit a PR, I can review and publish it...
no problem and thanks for your time but what did you mean with The index of the selected item will be passed as argument. ? if you have time
I meant that onChange will receive the index of the selected item after a change.
Have you seen the examples using storybook?
no
I just read the docs on npm
Check these demos: http://react-responsive-carousel.js.org/storybook/?selectedKind=Carousel&selectedStory=with%20external%20controls&full=0&down=1&left=1&panelRight=0&downPanel=kadirahq%2Fstorybook-addon-actions%2Factions-panel
You might find an example solving your problem if you look at the source of the stories
@leandrowd this is annoying you should show examples and provide basic changing of selected item handler code. People at least just want some default basic stuff to work out of the box like clicking the arrow to the next item.
@dschinkel please go through the readme and you will find all the answers to your questions. The carousel works out of the box of you follow the basic example and allows customisation when you need. If you find what you need in one of the storybooks, the code is available in the stories folder.
Same thing happens to me.
When I try to setState using the OnChange prop, the carousel resets after the second slide.
class Home extends React.Component {
state = {
selectedPage: 0,
};
setSelectedPage = index => {
console.log(('Index:', index));
this.setState({
selectedPage: index,
});
};
render() {
return (
<div>
<div className={s.home}>
<Carousel
showStatus={false}
showIndicators={false}
showThumbs={false}
onChange={this.setSelectedPage}
>
<div className={cx(s.slide, s.one)} />
<div className={cx(s.slide, s.two)} />
<div className={cx(s.slide, s.three)} />
<div className={cx(s.slide, s.two)} />
</Carousel>
</div>
<Navigation
selectedPage={this.state.selectedPage}
onMenuClick={this.setSelectedPage}
/>
</div>
);
}
}

I set the value as such for the onClickItem:
onClickItem={(index) => { this.clickBanner(index); }}
private clickBanner(index): void {
window.location.href = this.buildNewsLink(this.state.newsBannerItems[index]);
}
I'm not setting the state but in many other instances in React of the same process, works as expected.
The problem is with setState @Cyan005, but thanks for the input ;)
Alright yeah... I setup the exact same scenario and was able to recreate the issue you're seeing. Spent a few minutes debugging to see where the problem was living and where I'm getting stuck at (for now) is when a setState is declared within our onChange handler, the following block of code is being used in the Carousel component:
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.selectedItem !== this.state.selectedItem) {
this.updateSizes();
this.moveTo(nextProps.selectedItem);
}
When the setState on our end is not used, this is not being called. The nextProps.selectedItem is always 0. On the first click the condition is not met as both the nextProps.selectedItem is 0 and the this.state.selectedItem is 0. This works.
However, upon a subsequent onChange, the next pass the nextProps.selectedItem is 0 and the this.state.selectedItem is 1. This puts you into the condition block. And then it uses nextProps.selectedItem which is 0 to move back to position 0. It's as if the nextProps is not updating correctly.
I think there's a race condition here. setState is async and React batches them to improve performance.
I noticed that in this method
_this.selectItem = function (state) {
_this.setState(state);
_this.handleOnChange(state.selectedItem, _this.props.children[state.selectedItem]);
};
handleOnChange is being called before the state is actually set.
It should probably look something like this:
_this.selectItem = function (state) {
_this.setState(state, () => {
_this.handleOnChange(state.selectedItem, _this.props.children[state.selectedItem]);
});
};
But this doesn't really fix it. Will try to look into it more when I have time, but those are my two cents ;)
Yeah, I was all over those functions. Unfortunately through debugging, all of the numbers passed in and out of those were exactly as expected. The only 'off' thing I could find was the code block I pasted in my previous message so far.
If in the code you pasted you change the comparison to this, it works
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.selectedItem !== this.props.selectedItem) {
this.updateSizes();
this.moveTo(nextProps.selectedItem);
}
note I changed from state to props on the second part of the if statement