Custom tabs React native avoid re render
up vote
1
down vote
favorite
Is there a way to not re render tab content once you enter and go back after clicking another tab. The thing is that is always re render when I go to the other tab and i go back. I dont know if the solution may be related with pure components and shouldComponentUpdate but i don't know in which way use them
This is my code:
constructor() {
super();
this.state = {
user: null,
isLoaded: false,
isLoadedMyPosts: false,
tabSelected: 1,
myPosts: ,
favoritesPosts:
}
}
componentWillMount() {
storage.load({
key: "user",
}).then(userLocal => {
//Para tener la ultima informacion en los contadores
API.getUser(userLocal._id).then((data) => {
this.setState({ user: data[1], isLoaded: true })
})
API.getPostsByUser(userLocal._id).then((data) => {
this.setState({ myPosts: data, isLoadedMyPosts: true })
})
}).catch(err => {
// leave user null
this.setState({ isLoaded: true, isLoadedMyPosts: true })
return;
})
}
changeTab(value) {
switch (value) {
case 1:
this.setState({ tabSelected: 1 })
break;
case 2:
this.setState({ tabSelected: 2 })
storage.load({
key: "favorites",
}).then(favs => {
if (favs && favs.length) {
this.setState({ favoritesPosts: favs });
} else {
this.setState({ favoritesPosts: null });
}
}).catch(err => {
this.setState({ favoritesPosts: null });
return;
})
break;
}
}
And this is the important part of my render
{!this.state.isLoadedMyPosts &&
<View style={styles.loadingPosts}>
<ActivityIndicator size="large" color="#F5DA49" />
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 1 &&
//this.state.myPosts.map((item, index) => {
// return (<PostItem key={item._id} item={item} isTabFavorites={false} />)
// })
<FlatList
data={this.state.myPosts}
renderItem={({ item, separators }) => (
<PostItem key={item._id} item={item} isTabFavorites={false} />
)}
keyExtractor={item => item._id}
onEndReachedThreshold={0.5}
/>
}
{this.state.isLoadedMyPosts && this.state.myPosts.length === 0 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> {!this.state.user ? "Necesitas iniciar sesión" : "No tienes públicaciones"}</Text>
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 2 && this.state.favoritesPosts &&
this.state.favoritesPosts.map((item, index) => {
return (<PostItem key={item._id} item={item} isTabFavorites={true} removedFav={this.removedFav.bind(this)} />)
})}
{this.state.isLoadedMyPosts && !this.state.favoritesPosts && this.state.tabSelected === 2 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> No tienes favoritos</Text>
</View>
}
reactjs react-native
add a comment |
up vote
1
down vote
favorite
Is there a way to not re render tab content once you enter and go back after clicking another tab. The thing is that is always re render when I go to the other tab and i go back. I dont know if the solution may be related with pure components and shouldComponentUpdate but i don't know in which way use them
This is my code:
constructor() {
super();
this.state = {
user: null,
isLoaded: false,
isLoadedMyPosts: false,
tabSelected: 1,
myPosts: ,
favoritesPosts:
}
}
componentWillMount() {
storage.load({
key: "user",
}).then(userLocal => {
//Para tener la ultima informacion en los contadores
API.getUser(userLocal._id).then((data) => {
this.setState({ user: data[1], isLoaded: true })
})
API.getPostsByUser(userLocal._id).then((data) => {
this.setState({ myPosts: data, isLoadedMyPosts: true })
})
}).catch(err => {
// leave user null
this.setState({ isLoaded: true, isLoadedMyPosts: true })
return;
})
}
changeTab(value) {
switch (value) {
case 1:
this.setState({ tabSelected: 1 })
break;
case 2:
this.setState({ tabSelected: 2 })
storage.load({
key: "favorites",
}).then(favs => {
if (favs && favs.length) {
this.setState({ favoritesPosts: favs });
} else {
this.setState({ favoritesPosts: null });
}
}).catch(err => {
this.setState({ favoritesPosts: null });
return;
})
break;
}
}
And this is the important part of my render
{!this.state.isLoadedMyPosts &&
<View style={styles.loadingPosts}>
<ActivityIndicator size="large" color="#F5DA49" />
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 1 &&
//this.state.myPosts.map((item, index) => {
// return (<PostItem key={item._id} item={item} isTabFavorites={false} />)
// })
<FlatList
data={this.state.myPosts}
renderItem={({ item, separators }) => (
<PostItem key={item._id} item={item} isTabFavorites={false} />
)}
keyExtractor={item => item._id}
onEndReachedThreshold={0.5}
/>
}
{this.state.isLoadedMyPosts && this.state.myPosts.length === 0 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> {!this.state.user ? "Necesitas iniciar sesión" : "No tienes públicaciones"}</Text>
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 2 && this.state.favoritesPosts &&
this.state.favoritesPosts.map((item, index) => {
return (<PostItem key={item._id} item={item} isTabFavorites={true} removedFav={this.removedFav.bind(this)} />)
})}
{this.state.isLoadedMyPosts && !this.state.favoritesPosts && this.state.tabSelected === 2 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> No tienes favoritos</Text>
</View>
}
reactjs react-native
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Is there a way to not re render tab content once you enter and go back after clicking another tab. The thing is that is always re render when I go to the other tab and i go back. I dont know if the solution may be related with pure components and shouldComponentUpdate but i don't know in which way use them
This is my code:
constructor() {
super();
this.state = {
user: null,
isLoaded: false,
isLoadedMyPosts: false,
tabSelected: 1,
myPosts: ,
favoritesPosts:
}
}
componentWillMount() {
storage.load({
key: "user",
}).then(userLocal => {
//Para tener la ultima informacion en los contadores
API.getUser(userLocal._id).then((data) => {
this.setState({ user: data[1], isLoaded: true })
})
API.getPostsByUser(userLocal._id).then((data) => {
this.setState({ myPosts: data, isLoadedMyPosts: true })
})
}).catch(err => {
// leave user null
this.setState({ isLoaded: true, isLoadedMyPosts: true })
return;
})
}
changeTab(value) {
switch (value) {
case 1:
this.setState({ tabSelected: 1 })
break;
case 2:
this.setState({ tabSelected: 2 })
storage.load({
key: "favorites",
}).then(favs => {
if (favs && favs.length) {
this.setState({ favoritesPosts: favs });
} else {
this.setState({ favoritesPosts: null });
}
}).catch(err => {
this.setState({ favoritesPosts: null });
return;
})
break;
}
}
And this is the important part of my render
{!this.state.isLoadedMyPosts &&
<View style={styles.loadingPosts}>
<ActivityIndicator size="large" color="#F5DA49" />
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 1 &&
//this.state.myPosts.map((item, index) => {
// return (<PostItem key={item._id} item={item} isTabFavorites={false} />)
// })
<FlatList
data={this.state.myPosts}
renderItem={({ item, separators }) => (
<PostItem key={item._id} item={item} isTabFavorites={false} />
)}
keyExtractor={item => item._id}
onEndReachedThreshold={0.5}
/>
}
{this.state.isLoadedMyPosts && this.state.myPosts.length === 0 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> {!this.state.user ? "Necesitas iniciar sesión" : "No tienes públicaciones"}</Text>
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 2 && this.state.favoritesPosts &&
this.state.favoritesPosts.map((item, index) => {
return (<PostItem key={item._id} item={item} isTabFavorites={true} removedFav={this.removedFav.bind(this)} />)
})}
{this.state.isLoadedMyPosts && !this.state.favoritesPosts && this.state.tabSelected === 2 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> No tienes favoritos</Text>
</View>
}
reactjs react-native
Is there a way to not re render tab content once you enter and go back after clicking another tab. The thing is that is always re render when I go to the other tab and i go back. I dont know if the solution may be related with pure components and shouldComponentUpdate but i don't know in which way use them
This is my code:
constructor() {
super();
this.state = {
user: null,
isLoaded: false,
isLoadedMyPosts: false,
tabSelected: 1,
myPosts: ,
favoritesPosts:
}
}
componentWillMount() {
storage.load({
key: "user",
}).then(userLocal => {
//Para tener la ultima informacion en los contadores
API.getUser(userLocal._id).then((data) => {
this.setState({ user: data[1], isLoaded: true })
})
API.getPostsByUser(userLocal._id).then((data) => {
this.setState({ myPosts: data, isLoadedMyPosts: true })
})
}).catch(err => {
// leave user null
this.setState({ isLoaded: true, isLoadedMyPosts: true })
return;
})
}
changeTab(value) {
switch (value) {
case 1:
this.setState({ tabSelected: 1 })
break;
case 2:
this.setState({ tabSelected: 2 })
storage.load({
key: "favorites",
}).then(favs => {
if (favs && favs.length) {
this.setState({ favoritesPosts: favs });
} else {
this.setState({ favoritesPosts: null });
}
}).catch(err => {
this.setState({ favoritesPosts: null });
return;
})
break;
}
}
And this is the important part of my render
{!this.state.isLoadedMyPosts &&
<View style={styles.loadingPosts}>
<ActivityIndicator size="large" color="#F5DA49" />
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 1 &&
//this.state.myPosts.map((item, index) => {
// return (<PostItem key={item._id} item={item} isTabFavorites={false} />)
// })
<FlatList
data={this.state.myPosts}
renderItem={({ item, separators }) => (
<PostItem key={item._id} item={item} isTabFavorites={false} />
)}
keyExtractor={item => item._id}
onEndReachedThreshold={0.5}
/>
}
{this.state.isLoadedMyPosts && this.state.myPosts.length === 0 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> {!this.state.user ? "Necesitas iniciar sesión" : "No tienes públicaciones"}</Text>
</View>
}
{this.state.isLoadedMyPosts && this.state.tabSelected === 2 && this.state.favoritesPosts &&
this.state.favoritesPosts.map((item, index) => {
return (<PostItem key={item._id} item={item} isTabFavorites={true} removedFav={this.removedFav.bind(this)} />)
})}
{this.state.isLoadedMyPosts && !this.state.favoritesPosts && this.state.tabSelected === 2 &&
<View style={styles.noPosts}>
<Icon name="md-alert" size={40} />
<Text style={styles.textNoPosts}> No tienes favoritos</Text>
</View>
}
reactjs react-native
reactjs react-native
asked Nov 12 at 0:34
Franco Coronel
362211
362211
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254649%2fcustom-tabs-react-native-avoid-re-render%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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