0

When I click any menu item, it scrolls to the correct section. How to highlight the correct menu when I scroll to other section, like this?

product list category sticky menu

class List extends Component {
    constructor(props) {
      super(props);
      this.arr = [];
    }
    onPressFilter(val) {
        this.setState({ selectedTab: val });
        let dynamicIndex = categoryArr.indexOf(val);
        this.refs._scrollView.scrollTo({
            x: 0,
            y: this.arr[dynamicIndex] - 55,
            animated: true,
        });
    }
    render(){
        return (
            <ScrollView>
                <ScrollView ref='_scrollView'>
                    {{this.props.data.map((val, index) =>
                      <TouchableOpacity 
                        style={[styles.filterItem, (selectedTab === val) && {backgroundColor: themeColor}]}
                        onPress={this.onPressFilter.bind(this, val)} key={index}>
                          <Text style={[styles.filterText, (selectedTab === val) && {color: '#fff'}]}>{val.toUpperCase()}</Text>
                      </TouchableOpacity>)}}
                </ScrollView>
                {CategoryList.map((el, key) => (<View key={key}
                  onLayout={event => {
                    const layout = event.nativeEvent.layout;
                    this.arr[key] = layout.y;
                  }}>
                  <View style={{paddingHorizontal: 20, paddingVertical: 10}}>
                    <H2>{el.toUpperCase()}</H2>
                  </View>
                  <FlatList
                    data={DATA.filter((a, i) => a.category.name === el)}
                    renderItem={({ item }) => <Item item={item} data={DATA} />}
                    keyExtractor={item => item.id}
                  />
                </View>))}
            </ScrollView>
        )
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
khalifathul
  • 558
  • 1
  • 10
  • 24

1 Answers1

1

React native scrollview has a property onScroll. This promise returning the offset every time the user scrolls the layout. Based on the offset, you can set a range like between this range call the highlight function with a parameter which labels you want to highlight.

Hope this will help. Do you need more information, please don't hesitate to revert me back

Thanks

Dhevendhiran M
  • 994
  • 2
  • 12
  • 29