question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically add children at the beginning

See original GitHub issue

I’m trying to implement a Calendar (such as Google Calendar) and I’m having trouble with the swipe left feature.

Since I don’t want to load all the existing previous months since 1900, I simply want to add/load them dynamically. Let’s say I start with three months and set my initial index to be in the middle. The swiper will look like this: [ ] [ x ] [ ]

When I swipe to right, I need to add a month to my content, so I simply push a new month and the index moves by one, resulting in: [ ] [ ] [ x ] [ ]

The problem is when I swipe left. The index of the swiper will naturally move by one to the left, but I also need to add one new month at the beginning. If I use the event onMomentumScrollEnd to handle my logic, the index has already changed when I’m adding a new month at the beginning, which result in a bug. The index does not seem to be synced with its children.

I’m a bit loss on how I could implement this.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:16

github_iconTop GitHub Comments

5reactions
superandrew213commented, Dec 15, 2016

@ALWAL12 not sure if you still need this but I sort of came up with the a solution that is working quite well for me.

I use a swiper component with 3 screens, set initial screen to index 1 (middle screen).

          <Swiper
            loadMinimalSize={0}
            index={1}
            loadMinimal
            onScrollBeginDrag={(e, { index }, context) => this.setState({ swiperOldIndex: index })}
            onMomentumScrollEnd={(e, { index }, context) => this.setState({ swiperCurrentIndex: index })}
          >

            {this._renderScreenComponent()}

            {this._renderScreenComponent()}

            {this._renderScreenComponent()}

          </Swiper>

How it works is that when the user swipes you update the ScreenComponent with the new data. Essential you only have one ScreenComponent, the other two are used as placeholders so that the user can swipe and also to determine the swipe direction which you can then use to load the data for the ScreenComponent. To determine the direction I use this:

_getSwipeDirection(oldIndex, newIndex) {
    if (oldIndex === 0 && newIndex === 2) {
      return -1;
    } else if (oldIndex === 2 && newIndex === 0) {
      return 1;
    } else if (newIndex > oldIndex) {
      return 1;
    } else if (newIndex < oldIndex) {
      return -1;
    }
    return 0;
  }

Everytime you swipe, a loader appears before the component loads (if you turn this off then the previous screen component will show during the scroll until the scroll is completed then the direction can be determined and the ScreenComponent can be updated). The loader could be a deal breaker for some but for me it works quite well since I need to fetch data anyway.

0reactions
npaulConnvertexcommented, Oct 25, 2019
_getSwipeDirection(oldIndex, newIndex) {
    if (oldIndex === 0 && newIndex === 2) {
      return -1;
    } else if (oldIndex === 2 && newIndex === 0) {
      return 1;
    } else if (newIndex > oldIndex) {
      return 1;
    } else if (newIndex < oldIndex) {
      return -1;
    }
    return 0;
  }

When does the _“getSwipeDirection” get called in the component

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there anyway to dynamically add a child to a JSX element?
I tried something like: React.cloneElement( parent, props, child );. But that simply replaces the existing children with the new child ...
Read more >
DOM Manipulation: Adding new element to HTML dynamically
In this lecture, you will learn how to generate dynamic content for your webpage using JavaScript program. You will learn: 1.
Read more >
[Question] The orthodox way to dynamically add child DIVs to ...
In the code section add data to a list. In the UI section, place a loop in a parent div. Then loop over...
Read more >
Append child dynamically from JS in LWC
Whenever I try to append a child dynamically from Js in LWC component. I got this error. HTML : <template> <lightning-combobox name=" ...
Read more >
creating empty parents, loading children dynamically
What I want to do is create a top-level node without children, but it does have an arrow for opening the node. When...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found