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>
}









share|improve this question


























    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>
    }









    share|improve this question
























      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>
      }









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 0:34









      Franco Coronel

      362211




      362211





























          active

          oldest

          votes











          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',
          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%2f53254649%2fcustom-tabs-react-native-avoid-re-render%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.





          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.




          draft saved


          draft discarded














          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





















































          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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python