React Native not rendering on prop change

Can Poyrazoğlu

I have created the following component:

type ToggleButtonProps = { title: string, selected: boolean }
export default class ToggleButton extends Component<ToggleButtonProps>{
    render(){
        return (
            <TouchableWithoutFeedback {...this.props}>
                <View style={[style.button, this.props.selected ? style.buttonSelected : style.buttonDeselected]}>
                    <Text style={[style.buttonText, this.props.selected ? style.buttonTextSelected : style.buttonTextDeselected]}>{this.props.title}</Text>
                </View>
            </TouchableWithoutFeedback>
        );
    }
}

The styles are simple color definitions that would visually indicate whether a button is selected or not. From the parent component I call (item is my object):

item.props.selected = true;

I've put a breakpoint and I verify that it gets hit, item.props is indeed my item's props with a selected property, and it really changes from false to true.

However, nothing changes visually, neither do I get render() or componentDidUpdate called on the child.

What should I do to make the child render when its props change? (I am on React Native 0.59.3)

Tyro Hunter

You can't update the child component by literally assigning to props like this:

item.props.selected = true;

However, there are many ways to re-render the child components. But I think the solution below would be the easiest one.

You want to have a container or smart component which will keep the states or data of each toggle buttons in one place. Because mostly likely, this component will potentially need to call an api to send or process that data.

If the number of toggle buttons is fixed you can simply have the state like so:

state = {
  buttonOne: {
    id: `buttonOneId`,
    selected: false,
    title: 'title1'
  },
  buttonTwo: {
    id: `buttonTwoId`,
    selected: false,
    title: 'title2'
  },
}

Then create a method in the parent which will be called by each child components action onPress:

onButtonPress = (buttonId) => {
  this.setState({
    [buttonId]: !this.state[buttonId].selected // toggles the value
  }); // calls re-render of each child
}

pass the corresponding values to each child as their props in the render method:

render() {
  return (
     <View>
       <ToggleButton onPressFromParent={this.onButtonPress} dataFromParent={this.state.buttonOne} />
       <ToggleButton onPressFromParent={this.onButtonPress} dataFromParent={this.state.buttonTwo} />
       ...

finally each child can use the props:

  ...
  <TouchableWithoutFeedback onPress={() => this.props.onPressFromParent(this.props.dataFromParent.id)}>
    <View style={[style.button, this.props.dataFromParent.selected ? style.buttonSelected : style.buttonDeselected]}>
  ...

I left the title field intentionally for you to try and implement.

P.S: You should be able to follow the code as these are just JS or JSX.

I hope this helps :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React State change is not Rendering

React Native ListView row not re-rendering after state change

React native custom view, no propType for native prop

react native conditional rendering

Passing component as a prop in react native

react native - change prop state depending on number of buttons selected

React Native: Change style prop of 3rd party component

Component not rendering in react-native

React Native - invalid prop children

React child prop rendering before parent?

React Native - Modal not rendering

React Native: Flat List not rendering

Rendering change in component with new prop value?

React Native not rendering prop even though data is available

React Native FlatList Rendering

Conditionally Rendering in React Native

React Native rendering of componentDidMount

How to change StaticRouter's location prop using the <Link> tag in Server Side Rendering with React-Router

integer is not rendering in textbox in react native

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

Rendering in React-Native

Change state/prop in video react-native-video component

How to dynamically change the date for the date key in the items prop for react-native-calendars

React Native: TabView not re-rendering after change

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

React: Rendering a list of components by passing the list as a prop

Flatlist not rendering items (React Native)

Custom function not rendering on React Native

React Native change text color if prop is true or false