Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native styling. width: percentage - number

I want to do width: 100% - 50 so I can add an icon which is 50 wide on the right hand side of it.

I have got width: 100% - 20% working by using react-native-extended-styles but I don't see why that is useful because you can do width: '80%'. I cannot get width: 100% - 50 working. Is there a way?

Trying to use the onLayout event to get the container width, then set the <autocomplete> to 100% - 50 of the container width but it isn't working.

    let Location = (props) => {       let locationInputElement        const blur = () => {         locationInputElement.blur()       }        let inputContainerWidth        return (         <View style={styles.formItem}>           <View             onLayout={(event) => {               inputContainerWidth = event.nativeEvent.layout.width               console.log(inputContainerWidth)             }}      <Autocomplete               data={props.autocompleteResults.predictions}...  style={{             borderRadius: 8,             backgroundColor: 'red',             alignSelf: 'stretch',             paddingLeft: 10,             position: 'relative',             ...styles.label,             ...styles.labelHeight,             width: inputContainerWidth - 50           }}         />       </View>     </View>   ) } 

It does console.log 335 when it console.logs inputContainerWidth but the width of the <autocomplete> is 100% still.

like image 203
BeniaminoBaggins Avatar asked May 07 '17 00:05

BeniaminoBaggins


People also ask

How do I set dynamic width in React Native?

In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.

How do you use max width in React Native?

There is no such thing as "maxWidth" in React Native. You may want to style your component at run-time. Try playing with Dimensions . You can get screen width and screen height of the device and adjust width of your component accordingly.

How do you find the parent width of React Native?

To get the parent height and width in React: Set the ref prop on the element. In the useEffect hook, update the state variables for the height and width. Use the offsetHeight and offsetWidth properties to get the height and width of the element.


2 Answers

I'd agree with Viktor, you should be able to achieve this using Flex Box.

Here's something I put together: https://snack.expo.io/B1jDKOhyb

You set the flexDirection of the formRow to row, and then the first child (the holder View for your AutoComplete component to flex: 1. This makes it fill all available space. The next child View is your icon holder. Which you can set to whatever value you want (in this case 50).

export default class App extends Component {   render() {     return (       <View style={styles.container}>         <View style={styles.formRow}>           <View style={styles.formItem}>             // AutoComplete component goes here           </View>           <View style={styles.formIcon}>            // Icon goes here           </View>         </View>       </View>     );   } }  const styles = StyleSheet.create({   container: {     flex: 1,     paddingTop: 100   },   formRow: {     flexDirection: 'row',     height: 50,   },   formItem: {     flex: 1,     backgroundColor: 'dodgerblue',   },   formIcon: {     width: 50,     backgroundColor: 'greenyellow',   }, }); 
like image 152
Mike Avatar answered Sep 18 '22 14:09

Mike


This can easily be solved by using Dimensions.

import { Dimensions } from 'react-native';  const MyComponent = () => {   return <Text style={{height: Dimensions.get('window').height - 100}}></Text> };  export default MyComponent; 
like image 39
asbmin Avatar answered Sep 19 '22 14:09

asbmin