Reputation: 377
I'm new to React Native. I made a simple app with the latest version in a functional component.
I'm trying to achieve on leftside
icon onPress
right side scroll to that element and this is two siblings in screen you can name it with left side screen and right side screen left side screen with style={{flex:1}} and rightside style={{flex:2}}
The left side
has a list of 5 elements
and right side
screen have all 5 elements have 7 items on every list with title in a scroll view from react-native.
I want when the user clicks on the left side screen icon right side screen scrolls to the title position or something similar to that. The app is similar to when the user clicks on an item it scrolls to that item on the screen. I look into many tutorials not able to achieve what I'm trying to.
function leftside(){
<TouchableOpacity style={styles.left_container} onPress={itemA}>
<MaterialCommunityIcons
name={"itesname"}
size={35}
color={"#000"}
/>
<Text>Item</Text>
</TouchableOpacity>
<TouchableOpacity>
... till 5
</TouchableOpacity>
}
Right side:
import React from "react";
import { Text, View, Image } from "react-native";
function rightside({ uri, text }) {
return (
<View style={{ flexDirection: "column", backgroundColor: "#fff" }}>
<View
style={{
justifyContent: "center",
alignItems: "center",
paddingLeft: "5%",
}}
>
<Image
source={{
uri,
}}
style={{ width: 60, height: 80 }}
/>
<Text>{text}</Text>
</View>
</View>
);
}
export default rightside;
I tried to useRef
and pass the ref to scroll view not work for me or maybe I'm approaching it in the wrong way.
any reference to any blog or post or solution appreciated it.! Image reference here..
Upvotes: 4
Views: 687
Reputation: 433
The solution you are looking for can be achieved using Flatlist
<FlatList
ref={ref => {
this.flatListRef = ref;
}}
data={MainResults}
renderItem={({item, index}) => this._renderItemRight(item, index)}
keyExtractor={({id}) => id.toString()}
/>
To scroll to a particular index, the index of the item as done below:
scrollToIndex = index => { this.flatListRef.scrollToIndex({animated: true, index}); }; ;
To provide a solution to your question, I have created the entire feature including:
For better understanding of the solution, check out this gist
Note: The entire implementation is built as per this design(image for reference) which was provided in the question.
Flag my solution if you find it helpful. Cheers!
Upvotes: 1