How to show selected item in list with a different color in react native using native base ui kit?












0















I want to show the sidebar content in a different background color.For that I've tried TouchableOpacity underlay.But that is not the one I'm looking for.After giving TouchableOpacity ,it will change the color of the text only not the entire list background.How do I change the listitem background color as I'm using native base ui kit.Please help.Is there any method to do that?This is how the sidebar looks like.I've done something likes the following.Setting pressStatus as true within onPresList and if it is true change backround color.But navigation to route is not working.There is a mistake



https://i.stack.imgur.com/w9YiR.png



How do I change background color onPress? Following is my code.



updated






import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false
};
}
onPressList = (DATA, INDEX) => {

this.props.navigation.navigate(DATA.route);
this.setState({ pressStatus : true, selectedItem: INDEX});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
renderItem={({ DATA, INDEX }) => {
<ListItem
button
noBorder
onPress={() => this.onPressList(DATA, INDEX)}
style={{
backgroundColor:
this.state.selectedItem === INDEX ? "#cde1f9" : "transparent"
}}
>
<Left>
<Image
source={DATA.icon }
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{DATA.name}
</Text>
</Left>
</ListItem>}}
/>
</Content>
</Container>
);
}
}

export default SideBar;












share|improve this question

























  • Hi! do you mean the background color for entire left side menu?

    – Helmer Barcos
    Nov 21 '18 at 8:18













  • No I mean only the selected item within the list.Because for now, whenever I click on a item in sidebar it is not recognizable that an item is clicked.So I want to give a backround color change when clicked.

    – anu
    Nov 21 '18 at 8:28
















0















I want to show the sidebar content in a different background color.For that I've tried TouchableOpacity underlay.But that is not the one I'm looking for.After giving TouchableOpacity ,it will change the color of the text only not the entire list background.How do I change the listitem background color as I'm using native base ui kit.Please help.Is there any method to do that?This is how the sidebar looks like.I've done something likes the following.Setting pressStatus as true within onPresList and if it is true change backround color.But navigation to route is not working.There is a mistake



https://i.stack.imgur.com/w9YiR.png



How do I change background color onPress? Following is my code.



updated






import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false
};
}
onPressList = (DATA, INDEX) => {

this.props.navigation.navigate(DATA.route);
this.setState({ pressStatus : true, selectedItem: INDEX});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
renderItem={({ DATA, INDEX }) => {
<ListItem
button
noBorder
onPress={() => this.onPressList(DATA, INDEX)}
style={{
backgroundColor:
this.state.selectedItem === INDEX ? "#cde1f9" : "transparent"
}}
>
<Left>
<Image
source={DATA.icon }
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{DATA.name}
</Text>
</Left>
</ListItem>}}
/>
</Content>
</Container>
);
}
}

export default SideBar;












share|improve this question

























  • Hi! do you mean the background color for entire left side menu?

    – Helmer Barcos
    Nov 21 '18 at 8:18













  • No I mean only the selected item within the list.Because for now, whenever I click on a item in sidebar it is not recognizable that an item is clicked.So I want to give a backround color change when clicked.

    – anu
    Nov 21 '18 at 8:28














0












0








0








I want to show the sidebar content in a different background color.For that I've tried TouchableOpacity underlay.But that is not the one I'm looking for.After giving TouchableOpacity ,it will change the color of the text only not the entire list background.How do I change the listitem background color as I'm using native base ui kit.Please help.Is there any method to do that?This is how the sidebar looks like.I've done something likes the following.Setting pressStatus as true within onPresList and if it is true change backround color.But navigation to route is not working.There is a mistake



https://i.stack.imgur.com/w9YiR.png



How do I change background color onPress? Following is my code.



updated






import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false
};
}
onPressList = (DATA, INDEX) => {

this.props.navigation.navigate(DATA.route);
this.setState({ pressStatus : true, selectedItem: INDEX});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
renderItem={({ DATA, INDEX }) => {
<ListItem
button
noBorder
onPress={() => this.onPressList(DATA, INDEX)}
style={{
backgroundColor:
this.state.selectedItem === INDEX ? "#cde1f9" : "transparent"
}}
>
<Left>
<Image
source={DATA.icon }
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{DATA.name}
</Text>
</Left>
</ListItem>}}
/>
</Content>
</Container>
);
}
}

export default SideBar;












share|improve this question
















I want to show the sidebar content in a different background color.For that I've tried TouchableOpacity underlay.But that is not the one I'm looking for.After giving TouchableOpacity ,it will change the color of the text only not the entire list background.How do I change the listitem background color as I'm using native base ui kit.Please help.Is there any method to do that?This is how the sidebar looks like.I've done something likes the following.Setting pressStatus as true within onPresList and if it is true change backround color.But navigation to route is not working.There is a mistake



https://i.stack.imgur.com/w9YiR.png



How do I change background color onPress? Following is my code.



updated






import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false
};
}
onPressList = (DATA, INDEX) => {

this.props.navigation.navigate(DATA.route);
this.setState({ pressStatus : true, selectedItem: INDEX});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
renderItem={({ DATA, INDEX }) => {
<ListItem
button
noBorder
onPress={() => this.onPressList(DATA, INDEX)}
style={{
backgroundColor:
this.state.selectedItem === INDEX ? "#cde1f9" : "transparent"
}}
>
<Left>
<Image
source={DATA.icon }
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{DATA.name}
</Text>
</Left>
</ListItem>}}
/>
</Content>
</Container>
);
}
}

export default SideBar;








import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false
};
}
onPressList = (DATA, INDEX) => {

this.props.navigation.navigate(DATA.route);
this.setState({ pressStatus : true, selectedItem: INDEX});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
renderItem={({ DATA, INDEX }) => {
<ListItem
button
noBorder
onPress={() => this.onPressList(DATA, INDEX)}
style={{
backgroundColor:
this.state.selectedItem === INDEX ? "#cde1f9" : "transparent"
}}
>
<Left>
<Image
source={DATA.icon }
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{DATA.name}
</Text>
</Left>
</ListItem>}}
/>
</Content>
</Container>
);
}
}

export default SideBar;





import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false
};
}
onPressList = (DATA, INDEX) => {

this.props.navigation.navigate(DATA.route);
this.setState({ pressStatus : true, selectedItem: INDEX});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
renderItem={({ DATA, INDEX }) => {
<ListItem
button
noBorder
onPress={() => this.onPressList(DATA, INDEX)}
style={{
backgroundColor:
this.state.selectedItem === INDEX ? "#cde1f9" : "transparent"
}}
>
<Left>
<Image
source={DATA.icon }
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{DATA.name}
</Text>
</Left>
</ListItem>}}
/>
</Content>
</Container>
);
}
}

export default SideBar;






reactjs react-native jsx native-base






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:08







anu

















asked Nov 21 '18 at 7:35









anuanu

373526




373526













  • Hi! do you mean the background color for entire left side menu?

    – Helmer Barcos
    Nov 21 '18 at 8:18













  • No I mean only the selected item within the list.Because for now, whenever I click on a item in sidebar it is not recognizable that an item is clicked.So I want to give a backround color change when clicked.

    – anu
    Nov 21 '18 at 8:28



















  • Hi! do you mean the background color for entire left side menu?

    – Helmer Barcos
    Nov 21 '18 at 8:18













  • No I mean only the selected item within the list.Because for now, whenever I click on a item in sidebar it is not recognizable that an item is clicked.So I want to give a backround color change when clicked.

    – anu
    Nov 21 '18 at 8:28

















Hi! do you mean the background color for entire left side menu?

– Helmer Barcos
Nov 21 '18 at 8:18







Hi! do you mean the background color for entire left side menu?

– Helmer Barcos
Nov 21 '18 at 8:18















No I mean only the selected item within the list.Because for now, whenever I click on a item in sidebar it is not recognizable that an item is clicked.So I want to give a backround color change when clicked.

– anu
Nov 21 '18 at 8:28





No I mean only the selected item within the list.Because for now, whenever I click on a item in sidebar it is not recognizable that an item is clicked.So I want to give a backround color change when clicked.

– anu
Nov 21 '18 at 8:28












1 Answer
1






active

oldest

votes


















1














In the App example from native Base they dont support styles for background items list. so you should change your List component from NativeBase and add a FlatList Component from react native. but you should return the ListItem component from NativeBase. so Dont forget import { FlatList } from "react-native";



you should also modify the onPressList function (i would transform it into an arrow function)



in your states you should add the state selectedItem: 0



so every time you press an item, it would be trigger your function, that also modify a selectedItem idex, which tells the Flatlist, which Item should be have what background. i think this should really be the solution.



if it dont compile, make sure that you support arrow functions and that dont fehlt any curly braces or something like that.



Final Code UPDATE



import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false,
selectedItem:0
};
}

onPressList = (data, index) => {
this.props.navigation.navigate(data.route);
this.setState({ pressStatus : true, selectedItem: index});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
extraData={this.state.selectedItem}
renderItem={({item:data, index}) => {
const { selectedItem: sd } = this.state
const localColor ={backgroundColor: sd === index ? "#cde1f9" : "transparent"}
return (
<ListItem
button
noBorder
style={localColor}
onPress={() => this.onPressList(data, index)}
>
<Left>
<Image
source={data.icon}
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{data.name}
</Text>
</Left>
</ListItem>
)
}}
/>
</Content>
</Container>
);
}
}

export default SideBar;





share|improve this answer


























  • No I got this i.stack.imgur.com/CkBrR.png

    – anu
    Nov 21 '18 at 8:47











  • No still getting the same as before.How do I write code for color change onclick?

    – anu
    Nov 21 '18 at 8:58











  • ok i had to read the documentation. i hope the new change, is the solution

    – Helmer Barcos
    Nov 21 '18 at 9:36











  • let me know if it doesnt helped! :(

    – Helmer Barcos
    Nov 21 '18 at 9:49











  • I've edited my code please check.I'm getting error. cannot read property icon of undefined

    – anu
    Nov 21 '18 at 10:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407231%2fhow-to-show-selected-item-in-list-with-a-different-color-in-react-native-using-n%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In the App example from native Base they dont support styles for background items list. so you should change your List component from NativeBase and add a FlatList Component from react native. but you should return the ListItem component from NativeBase. so Dont forget import { FlatList } from "react-native";



you should also modify the onPressList function (i would transform it into an arrow function)



in your states you should add the state selectedItem: 0



so every time you press an item, it would be trigger your function, that also modify a selectedItem idex, which tells the Flatlist, which Item should be have what background. i think this should really be the solution.



if it dont compile, make sure that you support arrow functions and that dont fehlt any curly braces or something like that.



Final Code UPDATE



import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false,
selectedItem:0
};
}

onPressList = (data, index) => {
this.props.navigation.navigate(data.route);
this.setState({ pressStatus : true, selectedItem: index});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
extraData={this.state.selectedItem}
renderItem={({item:data, index}) => {
const { selectedItem: sd } = this.state
const localColor ={backgroundColor: sd === index ? "#cde1f9" : "transparent"}
return (
<ListItem
button
noBorder
style={localColor}
onPress={() => this.onPressList(data, index)}
>
<Left>
<Image
source={data.icon}
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{data.name}
</Text>
</Left>
</ListItem>
)
}}
/>
</Content>
</Container>
);
}
}

export default SideBar;





share|improve this answer


























  • No I got this i.stack.imgur.com/CkBrR.png

    – anu
    Nov 21 '18 at 8:47











  • No still getting the same as before.How do I write code for color change onclick?

    – anu
    Nov 21 '18 at 8:58











  • ok i had to read the documentation. i hope the new change, is the solution

    – Helmer Barcos
    Nov 21 '18 at 9:36











  • let me know if it doesnt helped! :(

    – Helmer Barcos
    Nov 21 '18 at 9:49











  • I've edited my code please check.I'm getting error. cannot read property icon of undefined

    – anu
    Nov 21 '18 at 10:09
















1














In the App example from native Base they dont support styles for background items list. so you should change your List component from NativeBase and add a FlatList Component from react native. but you should return the ListItem component from NativeBase. so Dont forget import { FlatList } from "react-native";



you should also modify the onPressList function (i would transform it into an arrow function)



in your states you should add the state selectedItem: 0



so every time you press an item, it would be trigger your function, that also modify a selectedItem idex, which tells the Flatlist, which Item should be have what background. i think this should really be the solution.



if it dont compile, make sure that you support arrow functions and that dont fehlt any curly braces or something like that.



Final Code UPDATE



import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false,
selectedItem:0
};
}

onPressList = (data, index) => {
this.props.navigation.navigate(data.route);
this.setState({ pressStatus : true, selectedItem: index});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
extraData={this.state.selectedItem}
renderItem={({item:data, index}) => {
const { selectedItem: sd } = this.state
const localColor ={backgroundColor: sd === index ? "#cde1f9" : "transparent"}
return (
<ListItem
button
noBorder
style={localColor}
onPress={() => this.onPressList(data, index)}
>
<Left>
<Image
source={data.icon}
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{data.name}
</Text>
</Left>
</ListItem>
)
}}
/>
</Content>
</Container>
);
}
}

export default SideBar;





share|improve this answer


























  • No I got this i.stack.imgur.com/CkBrR.png

    – anu
    Nov 21 '18 at 8:47











  • No still getting the same as before.How do I write code for color change onclick?

    – anu
    Nov 21 '18 at 8:58











  • ok i had to read the documentation. i hope the new change, is the solution

    – Helmer Barcos
    Nov 21 '18 at 9:36











  • let me know if it doesnt helped! :(

    – Helmer Barcos
    Nov 21 '18 at 9:49











  • I've edited my code please check.I'm getting error. cannot read property icon of undefined

    – anu
    Nov 21 '18 at 10:09














1












1








1







In the App example from native Base they dont support styles for background items list. so you should change your List component from NativeBase and add a FlatList Component from react native. but you should return the ListItem component from NativeBase. so Dont forget import { FlatList } from "react-native";



you should also modify the onPressList function (i would transform it into an arrow function)



in your states you should add the state selectedItem: 0



so every time you press an item, it would be trigger your function, that also modify a selectedItem idex, which tells the Flatlist, which Item should be have what background. i think this should really be the solution.



if it dont compile, make sure that you support arrow functions and that dont fehlt any curly braces or something like that.



Final Code UPDATE



import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false,
selectedItem:0
};
}

onPressList = (data, index) => {
this.props.navigation.navigate(data.route);
this.setState({ pressStatus : true, selectedItem: index});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
extraData={this.state.selectedItem}
renderItem={({item:data, index}) => {
const { selectedItem: sd } = this.state
const localColor ={backgroundColor: sd === index ? "#cde1f9" : "transparent"}
return (
<ListItem
button
noBorder
style={localColor}
onPress={() => this.onPressList(data, index)}
>
<Left>
<Image
source={data.icon}
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{data.name}
</Text>
</Left>
</ListItem>
)
}}
/>
</Content>
</Container>
);
}
}

export default SideBar;





share|improve this answer















In the App example from native Base they dont support styles for background items list. so you should change your List component from NativeBase and add a FlatList Component from react native. but you should return the ListItem component from NativeBase. so Dont forget import { FlatList } from "react-native";



you should also modify the onPressList function (i would transform it into an arrow function)



in your states you should add the state selectedItem: 0



so every time you press an item, it would be trigger your function, that also modify a selectedItem idex, which tells the Flatlist, which Item should be have what background. i think this should really be the solution.



if it dont compile, make sure that you support arrow functions and that dont fehlt any curly braces or something like that.



Final Code UPDATE



import React, { Component } from "react";
import { Image, FlatList } from "react-native";
import {
Content,
Text,
List,
ListItem,
Icon,
Container,
Left,
Right,
Badge,
Thumbnail
} from "native-base";
import styles from "./style";

const drawerCover = require("../../imgs/quwait.jpg");

const datas = [
{
name: "Dashboard",
route: "Anatomy",
icon: require("../../imgs/dashboard.png"),

},
{
name: "Companies",
route: "Header",
icon: require("../../imgs/enterprise1.png"),
},
{
name: "Company Admin",
route: "Footer",
icon: require("../../imgs/icon1.png"),

},
{
name: "Employee",
route: "NHBadge",
icon: require("../../imgs/businessman1.png"),

},
{
name: "Employs",
route: "NHButton",
icon: require("../../imgs/employee1.png"),

},
{
name: "Announcement",
route: "NHCard",
icon: require("../../imgs/megaphone1.png"),

},
{
name: "Holiday",
route: "Check",
icon: require("../../imgs/sun-umbrella1.png"),

},
{
name: "Accounting Report",
route: "NHTypography",
icon: require("../../imgs/accounting1.png"),

},
{
name: "Invoice",
route: "NHCheckbox",
icon: require('../../imgs/approve-invoice1.png'),

},
{
name: "Settings",
route: "NHDatePicker",
icon: require('../../imgs/settings1.png'),
},
{
name: "Safety Phone Numbers",
route: "NHThumbnail",
icon: "user",

},
{
name: "NBK",
route: "NHDeckSwiper",
icon: "swap",


},
{
name: "ABK",
route: "NHFab",
icon: "help-buoy",

},
{
name: "CBK",
route: "NHForm",
icon: "call",

},
{
name: "Daily Invoice",
route: "NHIcon",
icon: "information-circle",

},
{
name: "Kolin",
route: "NHLayout",
icon: "grid",

},
{
name: "Limak",
route: "NHList",
icon: "lock",

},
{
name: "Polaytol",
route: "ListSwipe",
icon: "code-working",

},
{
name: "ACTS",
route: "NHPicker",
icon: "arrow-dropdown",

}
];

class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
shadowOffsetWidth: 1,
shadowRadius: 4,
pressStatus:false,
selectedItem:0
};
}

onPressList = (data, index) => {
this.props.navigation.navigate(data.route);
this.setState({ pressStatus : true, selectedItem: index});
}

render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: "#fff", top: -1 }}
>
<Image source={drawerCover} style={styles.drawerCover} />
<FlatList
data={datas}
keyExtractor={(item, index) => String(index)}
extraData={this.state.selectedItem}
renderItem={({item:data, index}) => {
const { selectedItem: sd } = this.state
const localColor ={backgroundColor: sd === index ? "#cde1f9" : "transparent"}
return (
<ListItem
button
noBorder
style={localColor}
onPress={() => this.onPressList(data, index)}
>
<Left>
<Image
source={data.icon}
style={{width:30,height:30}}
/>
<Text style={styles.text}>
{data.name}
</Text>
</Left>
</ListItem>
)
}}
/>
</Content>
</Container>
);
}
}

export default SideBar;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 11:10

























answered Nov 21 '18 at 8:43









Helmer BarcosHelmer Barcos

624310




624310













  • No I got this i.stack.imgur.com/CkBrR.png

    – anu
    Nov 21 '18 at 8:47











  • No still getting the same as before.How do I write code for color change onclick?

    – anu
    Nov 21 '18 at 8:58











  • ok i had to read the documentation. i hope the new change, is the solution

    – Helmer Barcos
    Nov 21 '18 at 9:36











  • let me know if it doesnt helped! :(

    – Helmer Barcos
    Nov 21 '18 at 9:49











  • I've edited my code please check.I'm getting error. cannot read property icon of undefined

    – anu
    Nov 21 '18 at 10:09



















  • No I got this i.stack.imgur.com/CkBrR.png

    – anu
    Nov 21 '18 at 8:47











  • No still getting the same as before.How do I write code for color change onclick?

    – anu
    Nov 21 '18 at 8:58











  • ok i had to read the documentation. i hope the new change, is the solution

    – Helmer Barcos
    Nov 21 '18 at 9:36











  • let me know if it doesnt helped! :(

    – Helmer Barcos
    Nov 21 '18 at 9:49











  • I've edited my code please check.I'm getting error. cannot read property icon of undefined

    – anu
    Nov 21 '18 at 10:09

















No I got this i.stack.imgur.com/CkBrR.png

– anu
Nov 21 '18 at 8:47





No I got this i.stack.imgur.com/CkBrR.png

– anu
Nov 21 '18 at 8:47













No still getting the same as before.How do I write code for color change onclick?

– anu
Nov 21 '18 at 8:58





No still getting the same as before.How do I write code for color change onclick?

– anu
Nov 21 '18 at 8:58













ok i had to read the documentation. i hope the new change, is the solution

– Helmer Barcos
Nov 21 '18 at 9:36





ok i had to read the documentation. i hope the new change, is the solution

– Helmer Barcos
Nov 21 '18 at 9:36













let me know if it doesnt helped! :(

– Helmer Barcos
Nov 21 '18 at 9:49





let me know if it doesnt helped! :(

– Helmer Barcos
Nov 21 '18 at 9:49













I've edited my code please check.I'm getting error. cannot read property icon of undefined

– anu
Nov 21 '18 at 10:09





I've edited my code please check.I'm getting error. cannot read property icon of undefined

– anu
Nov 21 '18 at 10:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407231%2fhow-to-show-selected-item-in-list-with-a-different-color-in-react-native-using-n%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith