react-responsive-carousel icon indicating copy to clipboard operation
react-responsive-carousel copied to clipboard

selectedItem retains control when an `onChange()` sets it manually

Open mhesham32 opened this issue 8 years ago • 33 comments

<Carousel autoPlay={true} 
            interval={10000}
             showThumbs={false}
              infiniteLoop={true}
              showStatus={false}
              onChange={()=>this.setState({changed:!this.state.changed})}
              dynamicHeight={true}
             >

mhesham32 avatar Jan 02 '18 10:01 mhesham32

Yes, you can. The index of the selected item will be passed as argument.

leandrowd avatar Jan 02 '18 10:01 leandrowd

sorry I don't understand it doesn't work as I expected when I set the state can you explain more please?

mhesham32 avatar Jan 02 '18 10:01 mhesham32

What happens when you do it?

leandrowd avatar Jan 02 '18 10:01 leandrowd

the autoplay doesn't work and it only display the first two slides when I change them manually

mhesham32 avatar Jan 02 '18 10:01 mhesham32

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...

leandrowd avatar Jan 02 '18 10:01 leandrowd

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

mhesham32 avatar Jan 02 '18 10:01 mhesham32

I meant that onChange will receive the index of the selected item after a change.

leandrowd avatar Jan 02 '18 10:01 leandrowd

Have you seen the examples using storybook?

leandrowd avatar Jan 02 '18 10:01 leandrowd

no

mhesham32 avatar Jan 02 '18 10:01 mhesham32

I just read the docs on npm

mhesham32 avatar Jan 02 '18 10:01 mhesham32

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 avatar Jan 02 '18 10:01 leandrowd

@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 avatar Jan 06 '18 10:01 dschinkel

@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.

leandrowd avatar Jan 06 '18 10:01 leandrowd

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>
    );
  }
}

carousel

fxalvarezd avatar Jan 25 '18 22:01 fxalvarezd

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.

Cyan005 avatar Jan 26 '18 03:01 Cyan005

The problem is with setState @Cyan005, but thanks for the input ;)

fxalvarezd avatar Jan 26 '18 16:01 fxalvarezd

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.

Cyan005 avatar Jan 27 '18 01:01 Cyan005

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 ;)

fxalvarezd avatar Jan 27 '18 02:01 fxalvarezd

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.

Cyan005 avatar Jan 27 '18 02:01 Cyan005

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

fxalvarezd avatar Jan 27 '18 02:01 fxalvarezd