React-Native-Component not rendering when state is changed

Nidhi

I am making the show more and show less functionality inside a flat list but the state pressed is not working as expected .When I am setting the state value component is not being rendered when the state changes its value.

My constructor is set like below

this.state = {
            accList: [],
            expanded: false,
            expandedText: "Show More"
        }

In componentdidmount() I am updating the value of accList value like below

componentDidMount = () => {
      this.setState({
accList:[{
    "customer_name": "Shubhangi J Thakur",
    "message":"Hello"
},
{
    "customer_name": "Arthur S Campbell",
    "message":"Hello_World"
},
{

    "customer_name": "Susan R Brill",
    "message":"hellow"
}]
});

    }

I have defined the flatlist in render() like below

<FlatList
     onScroll={this.handleScroll}
     data={this.state.accList}
       renderItem={this.renderItem}
        keyExtractor={this._keyExtractor}
    />

    renderItem = ({ item, index }) => (
        <Card style={style.cardLayout} key={index}>
            <CardItem header>

              <Text>{item.customer_name}</Text>

            </CardItem>
            {this.seemorefunctionality(item)}

        </Card>
    );


    seemorefunctionality = (item) => {

              return <View>

            {this.state.expanded ? this.expandedView(item) :null}
//To view Show More and Show Less Text
                <TouchableOpacity onPress={this.expandedText}>
                 <Text> {this.state.expandedText}</Text>
                </TouchableOpacity>
              </View>

            }
          } 



expandedText = () => {
        console.log('Setting the expanded text value', this.state.expanded)
         if (this.state.expanded) {
           this.setState({
             expandedText: "Show More"
           });
         }
         else {
           this.setState({
             expandedText: "Show Less"
           });
         }
         value=!this.state.expanded
         this.setState({
           expanded: value

         });
       }


expandedView = (item) => {
        return   <View>
             {item.map((obj, index) => {
               return (
                 <View key={index} >
                 <Text>{obj.message}</Text>
                 </View>
               )
             })}
           </View>

When I am clicking on the this.state.expandedText value is getting changed when we see in the console but its not reflecting in the View also expandedView is not being rendered when this.state.expanded is set to true.
In View the value of this.state.expandedText is always showing Show More while I can see In console that the value is getting changed to Show more and Show Less on click

Shubham

for re-rendering flatlist you have to add extraData={this.state} as mention on https://facebook.github.io/react-native/docs/flatlist

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Component not re-rendering when state is changed

React component create a new instance of the state when re-rendering even the state is not changed

React component not refreshing when state changed

React - Component is rendered unnecessarily when state is changed

React component not rerendring when state is changed with Redux

React component is rendering but not updating when state is updating

React not re-rendering when state changed from hook

React Native state does not update immediately when state is changed

React component not rendering on route changed

React native: rendering conditional component based on state value change in Modal

my component is not re-rendering on state change react native

Prevent Child component getting rendered when the state is changed in react

react onChange on text field update whole component when state is changed

React Context and rerendering component when object state is changed

Trigger event when the state of child of another component changed in react

Why my React component does not rerender when Set state was changed?

React/react hooks: child component is not re-rendering after the state is changed?

React component not re-rendering when state changes

React component rendering even when there is no change in the state value

Is the react keep the old state in the component when rendering web page?

React Component does not re rendering when updating an object in state hooks

react-native open modal automatically when state is changed

React component not rendering on state update

React native component rendering differently when added as a child

Component not rendering in react-native

React native app is not rendering the component

Rendering specific component in React Native

React not rendering list after the state is changed

App is not rendering even when the state in the redux is changed

TOP Ranking

HotTag

Archive