Why React Native FlatList style not change with state












1















I have some tabs. I want to change the color of the selected tab. I created a state for selected tab index which will hold the tab ID. When Tab is pressed the selected state change to the pressed tab ID. I am comparing the selected state to tab ID. If both are equal then the selected tab will have some different style.



But when state changes and condition is true, the selected tab is not changing its state. Why change in state do not trigger the comparison in the style to update the style?






<FlatList 
horizontal
data={this.state.drinkgroup}
showsHorizontalScrollIndicator={false}
renderItem={({item, index}) =>
{
return <Tab
item={item}
selected={this.state.selected}
changeSelected={
() => {
this.setState({selected: item.id}, function(){ console.log(this.state.selected, item.id)
console.log(this.state.selected==item.id)
})
}}
}
}
/>








export const Tab = ({item, selected, changeSelected}) => {
return (
<TouchableOpacity
style={[styles.tabStyle, (selected==item.id)? styles.tabSelectedStyle: null]}
onPress={changeSelected}
underlayColor='#fff'
>
<Text style={{color: '#f2f2f2', textAlign: 'center', fontSize: 15}}>{item.name}</Text>
</TouchableOpacity>
);
}








const styles = StyleSheet.create({
tabStyle: {
backgroundColor: '#800080',
height: 32,
paddingRight: 15,
paddingLeft: 15
},
tabSelectedStyle: {
borderBottomColor: 'white',
borderBottomWidth: 3
}
})












share|improve this question





























    1















    I have some tabs. I want to change the color of the selected tab. I created a state for selected tab index which will hold the tab ID. When Tab is pressed the selected state change to the pressed tab ID. I am comparing the selected state to tab ID. If both are equal then the selected tab will have some different style.



    But when state changes and condition is true, the selected tab is not changing its state. Why change in state do not trigger the comparison in the style to update the style?






    <FlatList 
    horizontal
    data={this.state.drinkgroup}
    showsHorizontalScrollIndicator={false}
    renderItem={({item, index}) =>
    {
    return <Tab
    item={item}
    selected={this.state.selected}
    changeSelected={
    () => {
    this.setState({selected: item.id}, function(){ console.log(this.state.selected, item.id)
    console.log(this.state.selected==item.id)
    })
    }}
    }
    }
    />








    export const Tab = ({item, selected, changeSelected}) => {
    return (
    <TouchableOpacity
    style={[styles.tabStyle, (selected==item.id)? styles.tabSelectedStyle: null]}
    onPress={changeSelected}
    underlayColor='#fff'
    >
    <Text style={{color: '#f2f2f2', textAlign: 'center', fontSize: 15}}>{item.name}</Text>
    </TouchableOpacity>
    );
    }








    const styles = StyleSheet.create({
    tabStyle: {
    backgroundColor: '#800080',
    height: 32,
    paddingRight: 15,
    paddingLeft: 15
    },
    tabSelectedStyle: {
    borderBottomColor: 'white',
    borderBottomWidth: 3
    }
    })












    share|improve this question



























      1












      1








      1








      I have some tabs. I want to change the color of the selected tab. I created a state for selected tab index which will hold the tab ID. When Tab is pressed the selected state change to the pressed tab ID. I am comparing the selected state to tab ID. If both are equal then the selected tab will have some different style.



      But when state changes and condition is true, the selected tab is not changing its state. Why change in state do not trigger the comparison in the style to update the style?






      <FlatList 
      horizontal
      data={this.state.drinkgroup}
      showsHorizontalScrollIndicator={false}
      renderItem={({item, index}) =>
      {
      return <Tab
      item={item}
      selected={this.state.selected}
      changeSelected={
      () => {
      this.setState({selected: item.id}, function(){ console.log(this.state.selected, item.id)
      console.log(this.state.selected==item.id)
      })
      }}
      }
      }
      />








      export const Tab = ({item, selected, changeSelected}) => {
      return (
      <TouchableOpacity
      style={[styles.tabStyle, (selected==item.id)? styles.tabSelectedStyle: null]}
      onPress={changeSelected}
      underlayColor='#fff'
      >
      <Text style={{color: '#f2f2f2', textAlign: 'center', fontSize: 15}}>{item.name}</Text>
      </TouchableOpacity>
      );
      }








      const styles = StyleSheet.create({
      tabStyle: {
      backgroundColor: '#800080',
      height: 32,
      paddingRight: 15,
      paddingLeft: 15
      },
      tabSelectedStyle: {
      borderBottomColor: 'white',
      borderBottomWidth: 3
      }
      })












      share|improve this question
















      I have some tabs. I want to change the color of the selected tab. I created a state for selected tab index which will hold the tab ID. When Tab is pressed the selected state change to the pressed tab ID. I am comparing the selected state to tab ID. If both are equal then the selected tab will have some different style.



      But when state changes and condition is true, the selected tab is not changing its state. Why change in state do not trigger the comparison in the style to update the style?






      <FlatList 
      horizontal
      data={this.state.drinkgroup}
      showsHorizontalScrollIndicator={false}
      renderItem={({item, index}) =>
      {
      return <Tab
      item={item}
      selected={this.state.selected}
      changeSelected={
      () => {
      this.setState({selected: item.id}, function(){ console.log(this.state.selected, item.id)
      console.log(this.state.selected==item.id)
      })
      }}
      }
      }
      />








      export const Tab = ({item, selected, changeSelected}) => {
      return (
      <TouchableOpacity
      style={[styles.tabStyle, (selected==item.id)? styles.tabSelectedStyle: null]}
      onPress={changeSelected}
      underlayColor='#fff'
      >
      <Text style={{color: '#f2f2f2', textAlign: 'center', fontSize: 15}}>{item.name}</Text>
      </TouchableOpacity>
      );
      }








      const styles = StyleSheet.create({
      tabStyle: {
      backgroundColor: '#800080',
      height: 32,
      paddingRight: 15,
      paddingLeft: 15
      },
      tabSelectedStyle: {
      borderBottomColor: 'white',
      borderBottomWidth: 3
      }
      })








      <FlatList 
      horizontal
      data={this.state.drinkgroup}
      showsHorizontalScrollIndicator={false}
      renderItem={({item, index}) =>
      {
      return <Tab
      item={item}
      selected={this.state.selected}
      changeSelected={
      () => {
      this.setState({selected: item.id}, function(){ console.log(this.state.selected, item.id)
      console.log(this.state.selected==item.id)
      })
      }}
      }
      }
      />





      <FlatList 
      horizontal
      data={this.state.drinkgroup}
      showsHorizontalScrollIndicator={false}
      renderItem={({item, index}) =>
      {
      return <Tab
      item={item}
      selected={this.state.selected}
      changeSelected={
      () => {
      this.setState({selected: item.id}, function(){ console.log(this.state.selected, item.id)
      console.log(this.state.selected==item.id)
      })
      }}
      }
      }
      />





      export const Tab = ({item, selected, changeSelected}) => {
      return (
      <TouchableOpacity
      style={[styles.tabStyle, (selected==item.id)? styles.tabSelectedStyle: null]}
      onPress={changeSelected}
      underlayColor='#fff'
      >
      <Text style={{color: '#f2f2f2', textAlign: 'center', fontSize: 15}}>{item.name}</Text>
      </TouchableOpacity>
      );
      }





      export const Tab = ({item, selected, changeSelected}) => {
      return (
      <TouchableOpacity
      style={[styles.tabStyle, (selected==item.id)? styles.tabSelectedStyle: null]}
      onPress={changeSelected}
      underlayColor='#fff'
      >
      <Text style={{color: '#f2f2f2', textAlign: 'center', fontSize: 15}}>{item.name}</Text>
      </TouchableOpacity>
      );
      }





      const styles = StyleSheet.create({
      tabStyle: {
      backgroundColor: '#800080',
      height: 32,
      paddingRight: 15,
      paddingLeft: 15
      },
      tabSelectedStyle: {
      borderBottomColor: 'white',
      borderBottomWidth: 3
      }
      })





      const styles = StyleSheet.create({
      tabStyle: {
      backgroundColor: '#800080',
      height: 32,
      paddingRight: 15,
      paddingLeft: 15
      },
      tabSelectedStyle: {
      borderBottomColor: 'white',
      borderBottomWidth: 3
      }
      })






      react-native styles






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 12:07









      Harshal Valanda

      3,2561346




      3,2561346










      asked Jan 2 at 11:49









      Arun kumarArun kumar

      161416




      161416
























          2 Answers
          2






          active

          oldest

          votes


















          2














          By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.



          <FlatList
          data={this.props.data}
          extraData={this.state}
          keyExtractor={this._keyExtractor}
          renderItem={this._renderItem}
          />


          This is a PureComponent which means that it will not re-render if props remain shallow- equal.



          Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes.



          This includes the data prop and parent component state.



          More Details :- FlatList






          share|improve this answer































            1














            You need to provide extraData prop to the Flatlist, if you want it to re render its items. You can do it like extraData={this.state}.



            By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this extraData prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.
            For further details you can visit official documentation here






            share|improve this answer


























            • I am already proving the selected state as props in Tab component. Please explain further.

              – Arun kumar
              Jan 2 at 11:56











            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%2f54005825%2fwhy-react-native-flatlist-style-not-change-with-state%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.



            <FlatList
            data={this.props.data}
            extraData={this.state}
            keyExtractor={this._keyExtractor}
            renderItem={this._renderItem}
            />


            This is a PureComponent which means that it will not re-render if props remain shallow- equal.



            Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes.



            This includes the data prop and parent component state.



            More Details :- FlatList






            share|improve this answer




























              2














              By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.



              <FlatList
              data={this.props.data}
              extraData={this.state}
              keyExtractor={this._keyExtractor}
              renderItem={this._renderItem}
              />


              This is a PureComponent which means that it will not re-render if props remain shallow- equal.



              Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes.



              This includes the data prop and parent component state.



              More Details :- FlatList






              share|improve this answer


























                2












                2








                2







                By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.



                <FlatList
                data={this.props.data}
                extraData={this.state}
                keyExtractor={this._keyExtractor}
                renderItem={this._renderItem}
                />


                This is a PureComponent which means that it will not re-render if props remain shallow- equal.



                Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes.



                This includes the data prop and parent component state.



                More Details :- FlatList






                share|improve this answer













                By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.



                <FlatList
                data={this.props.data}
                extraData={this.state}
                keyExtractor={this._keyExtractor}
                renderItem={this._renderItem}
                />


                This is a PureComponent which means that it will not re-render if props remain shallow- equal.



                Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes.



                This includes the data prop and parent component state.



                More Details :- FlatList







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 11:57









                Harshal ValandaHarshal Valanda

                3,2561346




                3,2561346

























                    1














                    You need to provide extraData prop to the Flatlist, if you want it to re render its items. You can do it like extraData={this.state}.



                    By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this extraData prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.
                    For further details you can visit official documentation here






                    share|improve this answer


























                    • I am already proving the selected state as props in Tab component. Please explain further.

                      – Arun kumar
                      Jan 2 at 11:56
















                    1














                    You need to provide extraData prop to the Flatlist, if you want it to re render its items. You can do it like extraData={this.state}.



                    By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this extraData prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.
                    For further details you can visit official documentation here






                    share|improve this answer


























                    • I am already proving the selected state as props in Tab component. Please explain further.

                      – Arun kumar
                      Jan 2 at 11:56














                    1












                    1








                    1







                    You need to provide extraData prop to the Flatlist, if you want it to re render its items. You can do it like extraData={this.state}.



                    By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this extraData prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.
                    For further details you can visit official documentation here






                    share|improve this answer















                    You need to provide extraData prop to the Flatlist, if you want it to re render its items. You can do it like extraData={this.state}.



                    By passing extraData={this.state} to FlatList we make sure FlatList itself will re-render when the state.selected changes. Without setting this extraData prop, FlatList would not know it needs to re-render any items because it is also a PureComponent and the prop comparison will not show any changes.
                    For further details you can visit official documentation here







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 2 at 12:03

























                    answered Jan 2 at 11:54









                    kamrankamran

                    13619




                    13619













                    • I am already proving the selected state as props in Tab component. Please explain further.

                      – Arun kumar
                      Jan 2 at 11:56



















                    • I am already proving the selected state as props in Tab component. Please explain further.

                      – Arun kumar
                      Jan 2 at 11:56

















                    I am already proving the selected state as props in Tab component. Please explain further.

                    – Arun kumar
                    Jan 2 at 11:56





                    I am already proving the selected state as props in Tab component. Please explain further.

                    – Arun kumar
                    Jan 2 at 11:56


















                    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%2f54005825%2fwhy-react-native-flatlist-style-not-change-with-state%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