Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round button before rendering?

Tags:

react-native

I want to create a button component that will automatically have rounded corners, no matter its dimension.

As you know, to achieve rounded corners, one way to achieve it is to specify the border radius as half of the height of the button.

The way I implemented is that in the custom component I use the onLayout function like this:

 onLayout(event: LayoutChangeEvent) {
    const { height } = event.nativeEvent.layout;
    this.setState({ borderRadius: height / 2 });
  }

The problem is that the button will initially appear on screen as a rectangle and only after a millisecond, it will round the corners causing a flicker.

My guess is that onLayout is called after the component renders.

How would one go about implementing this? Thanks!

like image 423
Toma Radu-Petrescu Avatar asked Mar 16 '19 18:03

Toma Radu-Petrescu


People also ask

How do I round a button in CSS?

To create a rounded button you have to make use of the border-radius CSS property. The higher the value for that property the more rounder the corners will be. You can use any CSS unit for the boorder-radius property. It can be pixels, ems, rems, percentages etc.

How do you make a button rounded in react?

Button with rounded corner can be achieved by adding border-radius property. In the following example, e-round-corner class is added with border-radius as 5px .

How do I create a rounded button in bootstrap?

Rounded outline You can use . btn-outline-* and . btn-rounded together to make the button outline and rounded at the same time.


1 Answers

Before the borderRadius is calculated, you could return transparent button, this would prevent this flickering effect...

// you pass radius, and height from component state

const MyButton = ({ radius, height }) => {
   if (radius === null) return <View style={{ backgroundColor: transparent }}>...</View>

else return <View style={{ borderRadius: radius, backgroundColor: 'red' }}>...</View>;
};
like image 166
Hend El-Sahli Avatar answered Oct 20 '22 20:10

Hend El-Sahli