question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Button backgroundColor not changing after component rerendered

See original GitHub issue

I have gone through these following points

Issue Description

I tried to dynamically change the backgroundColor of a button by setting a variable as the value of the backgroundColor. Even though the component was rerendering, the styling value was not changing. I logged the value of variable below the user. As we can see, after pressing the Button, the component is rerendering and the value does change but the backgroundColor does not. image

After changing the button Component to a TouchableOpacity component and design the Button myself, it did work. image

node, npm, react-native, react and native-base version, expo version if used, xcode version

Node : 10.16.0 NPM : 6.10.2 React : 16.8.6 React-Native : 0.60.4 Native-Base : 2.13.0

Expected behaviour

The backgroundColor should change when the component is rerendering and the value of it is changing.

Actual behaviour

The backgroundColor did not change, the only way of doing so was to manually rerender the component by pressing the Display button to reshow the components.

Steps to reproduce

This is my Icon wrapped inside a Button component from native-base.

            <Button
                style={[
                  wallStyling.buttonIcon,
                  { backgroundColor: isMagnified },
                ]}
                onPress={() => this.handleMagnifyMessage(message)}
              >
                <Icon active name="md-resize" style={wallStyling.icon} />
              </Button>

The variable was defined as : const isMagnified = magnifiedMessage === message._id ? 'green' : '#0078FF'; This is the workaround I used, I had to create my own button Component by using a TouchableOpacity from react-native instead of a Button from native-base. It was working after using TouchableOpacity.

              <TouchableOpacity
                style={[
                  wallStyling.buttonIcon,
                  {
                    backgroundColor: isMagnified,
                    alignSelf: 'center',
                    color: '#fff',
                    paddingHorizontal: 17,
                    paddingVertical: 12,
                    justifyContent: 'center',
                  },
                ]}
                onPress={() => this.handleMagnifyMessage(message)}
              >
                <Icon active name="md-resize" style={wallStyling.icon} />
              </TouchableOpacity>

Is the bug present in both iOS and Android or in any one of them?

I only tried on Android.

Any other additional info which would help us debug the issue quicker.

I also tried to set the backgroundColor on the Icon directly but it didn’t change anything. To make it work using only an Icon, I had to wrap it in a View component and set the backgroundColor to it.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lwintjencommented, Aug 2, 2019

@PlabanJr It now works, thanks for your snippet. I guess it came from my version of native-base not up to date.

1reaction
PlabanJrcommented, Jul 31, 2019

@lwintjen @dgeorgiev I tried this in snack and is working fine. Please check once and if you can reproduce it make the changes to this snack and let me know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

backgroundColor doesn't re-render - React - Stack Overflow
i'm building a sorting algorithm visualizer. And every one of these items is a div with the backgroundColor set to white. enter image ......
Read more >
The last guide to the useEffect Hook you'll ever need
Based on a state, prop, or context change, the component will be re-rendered. If one or more useEffect declarations exist for the component, ......
Read more >
React JS - I need a button color to change onClick but cannot ...
The approach I am taking is to update the state for that component so that the button is re-rendered with a different background...
Read more >
Why is my child SwiftUI view not u… | Apple Developer Forums
I have a simple child view that contains a text view. Changing the state of my parent view should change that of my...
Read more >
Use CSS Variables instead of React Context - Epic React
I've never been so productive working with CSS than when I added the power ... to all emotion components without having to pass...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found