React Native: Unable to render components in a row with flex box - reactjs

I am using react-native-swipeout for items within a FlatList, each list item has a 'minus' and 'plus' button either side of the text (top image).
However, when adding these components as children of the Swipeout component, they are all stacking on top of each other and to the left of the row (bottom image).
Is there something I'm missing here?
<Swipeout
style={styles.swipeStyle}
{...swipeSettings}>
<View style={styles.buttonContainer}>
<MinusButton />
</View>
<View style={styles.itemStyle}>
<Text>{this.props.name}</Text>
</View>
<View style={styles.buttonContainer}>
<PlusButton />
</View>
</Swipeout>
const styles = {
swipeStyle: {
flex: 1,
flexDirection: 'row',
flexWrap: 'nowrap',
justifyContent: 'flex-start',
alignItems: 'flex-start'
},
buttonContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 5,
},
itemStyle: {
flex: 1,
}
};

If you look at the source code, there is another inner container that wraps the children and you cannot directly set style of the inner container.
I suggest wrap your children into a <View /> like:
<Swipeout {...swipeSettings}>
<View style={styles.swipeStyle}>
<View style={styles.buttonContainer}>
<MinusButton />
</View>
<View style={styles.itemStyle}>
<Text>{this.props.name}</Text>
</View>
<View style={styles.buttonContainer}>
<PlusButton />
</View>
</View>
</Swipeout>

Related

Unable to apply styling to react-native-progress-steps

I have been trying to apply styling to react-native-progress-steps but it's not working in my app. I applied the styles to View and ProgressSteps components but it's not showing in my app.
<View style={{flex: 1}}>
<ProgressSteps style={styles.progressStepsStyle}>
<ProgressStep label="Information">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 1!</Text>
</View>
</ProgressStep>
<ProgressStep label="Account">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 2!</Text>
</View>
</ProgressStep>
<ProgressStep label="Completion">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 3!</Text>
</View>
</ProgressStep>
<ProgressStep label="Verification">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 4!</Text>
</View>
</ProgressStep>
</ProgressSteps>
</View>
</View>
Here is the link to the library: https://www.npmjs.com/package/react-native-progress-steps
it is actually very simple.
You have to create a constant and call it inside the
who is the dad that locks up the ProgressStep.
For example:
const progressStepsStyle = {
activeStepIconBorderColor: '#0a0d64',
activeLabelColor: '#0a0d64',
activeStepNumColor: 'white',
activeStepIconColor: '#0a0d64',
completedStepIconColor: '#0a0d64',
completedProgressBarColor: '#0a0d64',
completedCheckColor: 'white'
};
And then call it:
<ProgressSteps {...progressStepsStyle}>
<ProgressStep label="Information">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 1!</Text>
</View>
</ProgressStep>
<ProgressStep label="Account">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 2!</Text>
</View>
</ProgressStep>
<ProgressStep label="Completion">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 3!</Text>
</View>
</ProgressStep>
<ProgressStep label="Verification">
<View style={{ alignItems: 'center' }}>
<Text>This is the content within step 4!</Text>
</View>
</ProgressStep>
</ProgressSteps>
So you will have styles in your circles and in the progress bar.
If you want to style your buttons and button text you have to place the following properties inside
nextBtnStyle, nextBtnTextStyle, previousBtnStyle, and previousBtnTextStyle.
For example:
<ProgressStep nextBtnText={"Next"} nextBtnStyle={{ textAlign: 'center', padding: 8, backgroundColor: '#F8CC23', }} nextBtnTextStyle={{ color: '#007aff', fontSize: 18 }}>

Text out of view in Touchable opacity React native

I'm new to react native and i'm trying to display some text in a custom touchable opacity. but the text keep exceeding the border even when i'm using flex wrap. my code is the following:
<View style={styles.main_container} >
<View style={styles.ctr1}>
<TouchableOpacity style={{ flexDirection: 'row' }} >
<Image style={styles.img} source={{ uri:`data:image/gif;base64,${encodedData}`}}/>
<View>
<Text style={styles.txt}> k </Text>
<Text style={{ flexWrap:1 }} > ddddddddddddddddddddddddddddddddddddddddd </Text>
<Text> kk </Text>
</View>
</TouchableOpacity>
</View>
</View>
and here is look of the styles i used:
main_container: {
flex: 1,
height: 300,
flexDirection: 'column',
backgroundColor: "grey",
width: '95%',
margin: 10,
},
ctr1: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'white',
//margin: 2,
},
How can i wrap it
Issue was not in the <Text> element that you have given the style flexWrap:1. Problem was in the parent elements.
Adding flex: 1 to <TouchableOpacity> and <View> which contains your <text> will solve the problem.
This will do the calculation for width depends on the device width.
<View style={styEdit.main_container}>
<View style={styEdit.ctr1}>
<TouchableOpacity style={{ flexDirection: 'row', flex: 1 }} >
<Image style={styles.img} source={{ uri:`data:image/gif;base64,${encodedData}`}}/>
<View style={{ flexDirection: 'column', flex: 1 }}>
<Text style={styEdit.txt}>k</Text>
<Text>ddddddddddddddddddddddddddddddddddddddddd---</Text>
<Text>kk</Text>
</View>
</TouchableOpacity>
</View>
</View>
Tested the code in my machine.
Cheers!

float right element in react native

how can switch red background with green
my code is:
<View style={{flexDirection:'row', justifyContent: 'flex-end', alignItems: 'flex-end'}}>
<View style={{backgroundColor:'red', flex: 1, justifyContent: 'flex-end'}}>
<Text>1</Text>
</View>
<View style={{backgroundColor:'blue', flex: 1}}>
<Text>2</Text>
</View>
<View style={{backgroundColor:'green', flex: 1}}>
<Text>3</Text>
</View>
</View>
</View>
How to fix the problem by changing the style?
please help
try to use something as a flag to change the style of your view

Space between components in React Native styling

I have 6 View components (shown in the picture) , I want to have space between all 6 View components.
My code:
<View style={{flexDirection:'column',flex:6}}>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between'}}>
<View style={{backgroundColor:'red',flex:1}}>
</View>
<View style={{backgroundColor:'blue',flex:1}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between'}}>
<View style={{backgroundColor:'white',flex:1}}>
</View>
<View style={{backgroundColor:'black',flex:1}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between'}}>
<View style={{backgroundColor:'gray',flex:1}}>
</View>
<View style={{backgroundColor:'yellow',flex:1}}>
</View>
</View>
</View>
Try to add padding to the element then and another blank view in each row, padding make space between each item
<View style={{
flexDirection:'column',
flex:1,
}}>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between',padding:10}}>
<View style={{backgroundColor:'red',flex:2,padding:10}}>
</View>
<View style={{flex:0.1}}/>
<View style={{backgroundColor:'blue',flex:2,padding:10}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between',padding:10}}>
<View style={{backgroundColor:'white',flex:2,padding:10}}>
</View>
<View style={{flex:0.1}}/>
<View style={{backgroundColor:'black',flex:2,padding:10}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between',padding:10}}>
<View style={{backgroundColor:'gray',flex:1,padding:10}}>
</View>
<View style={{flex:0.1}}/>
<View style={{backgroundColor:'yellow',flex:1,padding:10}}>
</View>
</View>
You can simply add margins to the elements and it will work fine.
<View style={{flexDirection:'column',flex:6}}>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between', marginBottom: 10}}>
<View style={{backgroundColor:'red',flex:1, marginRight: 5}}>
</View>
<View style={{backgroundColor:'blue',flex:1, marginLeft: 5}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between', marginBottom: 10}}>
<View style={{backgroundColor:'white',flex:1, marginRight: 5}}>
</View>
<View style={{backgroundColor:'black',flex:1, marginLeft: 5}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",justifyContent:'space-between', marginBottom: 10}}>
<View style={{backgroundColor:'gray',flex:1, marginRight: 5}}>
</View>
<View style={{backgroundColor:'yellow',flex:1, marginLeft: 5}}>
</View>
</View>
</View>
As of React Native 0.71.0, you can use the gap property. These child views will have a gap of 10 pixels between each row / column.
<View style={{flexDirection:'column', gap: 10 }}>
<View />
<View />
<View />
<View />
<View />
<View />
</View>
If you are using the map function to render your components, you can try the following code:
// Change to the number of columns in your grid
const numCols = 4;
// Change to the spacing for each item
const spacing = '1.25rem';
// Parent View
<View style={{
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
}}
>
{// List of child views to render
contents.map((content, index) => (
// add a margin of 'spacing' to the bottom of all cards
// will supply a left-margin to all cards in between the gap,
// everytime a card is NOT divisible numCols, add a margin to the left
<View style={{
marginBottom : spacing,
marginLeft: index % numCols !== 0 ? spacing : 0
}}
>
<Card
key={index}
content={content}
/>
</View>
))
}
</View>
I found that using the following code snippet will create an effect similar to gap in css grids.
Give the following styling to the parent container (Block/View).
justifyContent: 'space-around'
OR
justifyContent: 'space-between'
Space-around will put space between all the elements/view/items and also add space to the borders. Same as all elements get the margin.
And the space-between will put the space between all items, without adding space to the borders
All this stuff is explained here beautifully. Here is the link
Try this
const Component = () => {
return (
<View style={styles.container}>
<View style={styles.child}></View>
<View style={styles.child}></View>
</View>
);
};
// Gap you want to achieve
const gap = 8;
const styles = StyleSheet.create({
container: {
flexDirection: "row",
paddingHorizontal: (gap / -2),
},
child: {
marginHorizontal: gap / 2,
},
});
For me, the solution, among using justify-content: 'space-between', was to explicitly give width to the parent view (in my case, width: '100%'), otherwise, 'space-between' didn't do anything.
you have 2 solutions in this case
first, use margin/padding for make air between flex element
and the other way is set justifyContent to 'space-between' || 'space-around'
but you must know about some information to use the best solution in a different case
1- (margin-padding)
in some case (more case), padding better margin
as you know, if we have a box ( means element ), you have two space,
first between content and border [ Inner space ]
second, space between the border and another foreign box element [ outer space ]
if you have an element and you want move element in your style you must use margin, because if you use padding for your element, inner space changed and in many cases, this approach creates some crash in your element,
{{ if you want to use padding you must create one View and wrap all of your element and set the padding for a wrapper for you }}
and if you want to use space-between || space-around, you must know this,
space-between and space-around have one diff
in space-between, space produced between elements and not create space between your element with sides
and space-around set space with elements and with sides
You can give the parent a negative margin in the same direction you use on the children.
const Comp: FC = () => {
return (
<View style={styles.container}>
<View style={styles.item}></View>
<View style={styles.item}></View>
<View style={styles.item}></View>
<View style={styles.item}></View>
</View>
)
}
const styles = StyleSheet.create({
item: {
// Just for visual representation:
borderWidth: 2,
borderColor: '#FF0000',
height: 100,
// Important styles:
marginRight: 15,
flexBasis: '40%',
flexGrow: 1,
},
container: {
// Important styles:
marginRight: -15,
flexDirection: 'row',
flexWrap: 'wrap',
},
});
export default Comp;
<View style={{ flexDirection: "row", flexWrap: "wrap" }}>
{Array(30)
.fill({ name: "product name" })
.map((item, key, arr) => (
<View
key={key}
style={{
height: 200,
paddingTop: 15,
backgroundColor: "skyblue",
...(key % 2 !== 0
? { paddingRight: 15, paddingLeft: 15 }
: { paddingLeft: 15 }),
flexBasis: "50%",
}}
>
<View
style={{
flex: 1,
backgroundColor: variables?.BackgroundLight,
}}
>
<Text style={styles && { ...styles?.text }}>{item.name}</Text>
</View>
</View>
))}
</View>
Try this out. You can better control it using array map
If you want to put some vertical space, without flex, this is simple (just for temporary use)
<View style={{flex:2,flexDirection:"row",flexWrap: 'wrap'}}>
<View style={{backgroundColor:'white',flex:1,marginHorizontal:5,marginVertical:10,}}>
</View>
<View style={{backgroundColor:'black',flex:1,marginHorizontal:5,marginVertical:10,}}>
</View>
</View>
<View style={{flex:2,flexDirection:"row",flexWrap: 'wrap'}}>
<View style={{backgroundColor:'gray',flex:1,marginHorizontal:5,marginVertical:10,}}>
</View>
<View style={{backgroundColor:'yellow',flex:1,marginHorizontal:5,marginVertical:10,}}>
</View>
</View>
You can add the following in your style:
attachcontainer{
flexDirection: 'row',
justifyContent: 'space-between'
}

Why is "flex"-Property not working any more in react-native?

With React-Native 0.26 i could style a screen with a header and a large panel in proportion 1:10 when passing "flex:1" to the header and "flex:9" to the other view.
However, with newest React-Native > 0.40 this is not working any more.
What is wrong with my styles?
This is the render function:
render() {
return (
<View style={s.container}>
<View style={s.progressBar}>
<Progress.Bar progress={this.state.progress} width={200} />
</View>
<ListView
style={s.listView}
enableEmptySections={true}
keyboardShouldPersistTaps="always"
keyboardDismissMode="on-drag"
dataSource={this.state.dataSource}
renderRow={(contact) => this._renderContact(contact)}
/>
</View>
)
}
These are the styles:
const s = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
progressBar: {
flex: 1,
backgroundColor: 'lightgray',
height: 20,
alignSelf: 'stretch',
},
listView: {
flex: 10,
alignSelf: 'stretch',
}
});
This happens now:
Wether flex: 1 is working nor the height-property. It does not matter if these properties are passed or not. It always divides the screen into 2 subviews with the same height:
What i want to achieve is to give the gray area a height of either 20 pixel or 10 percent of the screen.
You should wrap ListView tag inside a View and put the style on the View instead:
<View style{{flex: 1}}>
<View style={{flex: 1}}>
<Text>This is my header</Text>
</View>
<View style={{flex: 10}}>
<ListView
dataSource={YOUR_SOURCE}
renderRow={YOUR_RENDER_CALLBACK}
/>
</View>
</View>
For the scrollview try passing styles using the "contentContainerStyle" prop

Resources