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.

Dynamic backgroundColor is not changing after rerendering

See original GitHub issue

Environment

React-Native : 0.60.4 React-Native-Vector-Icons : 6.6.0

Description

I tried to dynamically change the backgroundColor of the icon 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 Icon, the component is rerendering and the value does change but the backgroundColor does not. image

I found a workaround to this, I wrapped my component inside a View and set the backgroundColor variable to the view component. And after doing this, the backgroundColor was finally changing. image

Reproducible Demo

Here is the code used :

                <Icon
                  name="md-resize"
                  size={40}
                  color="white"
                  style={{
                    alignSelf: 'center',
                    color: '#fff',
                    backgroundColor: 'red',
                    paddingHorizontal: 17,
                    paddingVertical: 12,
                  }}
                  onPress={() => this.handleMagnifyMessage(message)}
                />

My variable was defined as const isMagnified = magnifiedMessage === message._id ? 'green' : '#0078FF';

And the workaround I used to make it work is :

              <View style={{ backgroundColor: isMagnified }}>
                <Icon
                  name="md-resize"
                  size={40}
                  color="white"
                  style={{
                    alignSelf: 'center',
                    color: '#fff',
                    paddingHorizontal: 17,
                    paddingVertical: 12,
                  }}
                  onPress={() => this.handleMagnifyMessage(message)}
                />
              </View>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
lwintjencommented, Aug 8, 2019

You’re right, my bad. The {} around color were causing the issue. Sorry.

0reactions
hampustagerudcommented, Aug 8, 2019

No worries, glad it’s working 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Opacity not changing when component rerenders in ...
When this variable is true, the opacity of the button should be only 0.5. When the value goes to false, then the opacity...
Read more >
React: How To Change Background Color Dynamically On Click
react #reactjs #webdev #coding #programming how to change background color in react dynamically ? This tutorial will show you How To Change ......
Read more >
When to use useCallback, useMemo and useEffect
The reason behind this behavior is that when we press the button, the app component is rerendered, and hence the function getItems() inside...
Read more >
How to stop re-rendering lists in React? - Alex Sidorenko
There is a common misconception that a React component will not re-render unless one of its properties changes. This is not true:.
Read more >
Documentation - SolidJS · Reactive Javascript Library
The memo function should not change other signals by calling setters (it ... This allows for dynamic tracking of properties that aren't present...
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