How to apply absolute position to custom Component in React Native? - react-native

this something stange but as you can see here: https://snack.expo.io/B1_5TLFvW the custom component "Cualquiera" is not positioning with absolute position, it's showing in stack, just like a list, and View components are good, if I change "class Cualquiera extends to Component" to "class Cualquiera extends View" it then it takes the position correctly, I have a custom component in my code that is not taking the absolute positions even if I set it to extend View, it shows the components as list too. How can I make it work with absolute positions extending Component?
thank you!!
here the code
import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
export default class Position extends Component {
render() {
return (
<View style={styles.container}>
<Cualquiera numero={1} style={styles.box1} />
<Cualquiera numero={4} style={styles.box4} />
<View numero={2} style={styles.box2}><Text style={styles.text}>2</Text></View>
<View numero={3} style={styles.box3}><Text style={styles.text}>3</Text></View>
</View>
);
}
}
class Cualquiera extends Component {
render() {
return (
<View
style={{
backgroundColor: '#49f',
borderWidth: 0.5,
borderColor: '#f05',
padding: 20,
}}>
<Text style={styles.text}>{this.props.numero}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
// flex: 1
},
box1: {
position: 'absolute',
top: 40,
left: 40,
width: 100,
height: 100,
backgroundColor: 'red',
},
box2: {
position: 'absolute',
top: 80,
left: 80,
width: 100,
height: 100,
backgroundColor: 'blue',
},
box3: {
position: 'absolute',
top: 120,
left: 120,
width: 100,
height: 100,
backgroundColor: 'green',
},
box4: {
position: 'absolute',
top: 160,
left: 160,
width: 100,
height: 100,
backgroundColor: 'yellow',
},
text: {
color: '#ffffff',
fontSize: 40,
},
});

The Cualquiera component is a custom component. The styles prop you pass into it isn't used for styling. If you want to pass in styles, pass in the style prop into view style prop like this:
class Cualquiera extends Component {
render() {
return (
<View
style={[
{ backgroundColor: 'blue'}, // pass in your main styles here
this.props.style // styles passed by props will be used
]}
/>
)
}

Related

react-native-image-slider-box with dotStyle above image Slider not working

In this example i have used react-native-image-slider here i want to show image pagination dots at left side bottom image slider box how to do like that.
SliderBoxExample.js
import React, { Component } from 'react';
import {Image, Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { SliderBox } from "react-native-image-slider-box";
export default class SliderBoxExample extends Component {
constructor(props) {
super(props);
this.state = {
images: [
require('./../assets/images/cat.jpg'),
require('./../assets/images/dao.jpg'),
require('./../assets/images/monkey.jpg'),
]
};
}
render() {
const { navigation } = this.props
return (
<View style={styles.container}>
<SliderBox
ImageComponent={Image}
images={this.state.images}
sliderBoxHeight={200}
dotColor="red"
inactiveDotColor="#90A4AE"
paginationBoxVerticalPadding={20}
autoplay
circleLoop
resizeMethod={'resize'}
resizeMode={'cover'}
paginationBoxStyle={{
position: "absolute",
bottom: 0,
padding: 0,
alignItems: "center",
alignSelf: "center",
justifyContent: "center",
paddingVertical: 10
}}
dotStyle={{
width: 10,
height: 10,
borderRadius: 5,
marginHorizontal: 0,
padding: 0,
margin: 0,
backgroundColor: "rgba(128, 128, 128, 0.92)"
}}
ImageComponentStyle={{borderRadius: 15, width: '97%', marginTop: 25}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
Suggest any solution if you have..
Thanks.
Image change pagination at left side below image slider box..
Windas_Coder! I'm not sure if I got it, but what I understood was that you want to move the pagination dots to the bottom-left side of the content box. An easy way of doing that is to set alignSelf to flex-start inside the property paginationBoxStyle. Something like that:
paginationBoxStyle={{
alignSelf: "flex-start",
}}
So as you can read more about here, you can:
apply this property to a single child to change its alignment within its parent.
Or in another words, change only the position of the pagination box, inside the SliderBox using flex properties.
remove slider box dot.........
<SliderBox
images={this.state.images}
sliderBoxHeight={200}
onCurrentImagePressed={index => console.warn(`image ${index} pressed`)}
dotColor="#FFEE58"
inactiveDotColor="#90A4AE"
dotStyle={{
width: 0,
height:0,
borderRadius:0,
marginHorizontal:0,
padding: 0,
margin: 0
}}
/>

Why won't my top level react-native component expand to the full dimensions?

I'm using next.js if that matters and my pages/_app.tsx has:
function MyApp({ Component, pageProps }: AppProps) {
return (
<PaperProvider theme={customTheme}>
<View style={{ flex: 1, top: 0, left: 0, height: '100%', width: '100%', zIndex: 10, position: 'absolute', backgroundColor: 'red' }}>
<Text> Here</Text>
</View>
</PaperProvider>
)
}
export default MyApp
and customTheme is:
import { DefaultTheme } from 'react-native-paper';
export const customTheme = {
...DefaultTheme,
dark: false,
colors: {
...DefaultTheme.colors,
primary: '#247BA0',
accent: '#70C1B3',
error: '#FF1654',
disabled: '#F3FFBD',
placeholder: '#D3EDBE',
}
}
I see Text, but no red background. What am I missing?
Ok the problem here is the use of position: 'absolute' with height: '100%', delete one of these properties in your style object and it should work.
If you want the full screen to have background color change height: '100%' to height: Dimensions.get("window").height

React Native draw custom rounded edge on View

I want to create a View which has one rounded edge on it in react native as you can see in the image. I already have the white view as I want it to be. However, it should extend up to the black curve. What are my options and how can it be achieved?
Thank you in advance.
You can make the white view elliptical by adding a bottom border radius and scaling it up using the transform attribute.
Then if you place it partially above the second view, you will end up with the expected output.
Here is an example:
import * as React from 'react';
import { View, StyleSheet } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.first} />
<View style={styles.second} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
width: '100%',
backgroundColor: '#fff',
},
first: {
backgroundColor: '#fff',
alignSelf: 'center',
width: 100,
height: 100,
borderBottomRightRadius: 50,
borderBottomLeftRadius: 50,
transform: [{ scaleX: 4 }],
zIndex: 1,
},
second: {
backgroundColor: '#333',
marginTop: -30,
height: 200,
zIndex: 0,
},
});
Here is the example in action.

Animated.View and Animated.Value throwing readonly assignment or immutable object error

I have what I thought was a very straightforward usage of Animated.View and Animated.Value to control the position of an slide-open menu bar. I have tried to place the Animated.timing() in the constructor, componentWillMount, componentDidMount; none of those options have worked. The error with the blow configuration is: Attempted to assign readonly property
If I move the Animated.timing() to the constructor I get: you attempted to set the key on an object that is meant to be immutable.
Here is my current code:
import React, { Component } from 'react'
import {
View,
ScrollView,
Text,
StyleSheet,
TouchableOpacity,
Animated,
Easing,
} from 'react-native'
import _style from '../../masterStyle'
export default class Nav extends Component {
constructor(props){
super(props);
this.state = {
navPosition: new Animated.Value(-230)
};
Animated.timing(this.state.navPosition, {
toValue: 0,
easing: Easing.back(),
}).start(()=>{
console.log("did start")
});
}
componentDidMount(){
}
render(){
return (
<Animated.View style={style.navWrap}>
<TouchableOpacity style={style.navMask} onPress={this.props.hide}>{}</TouchableOpacity>
<ScrollView style={[style.menuWrap,{right: this.state.navPosition}]}>
<Text style={_style.h2}>{}</Text>
</ScrollView>
</Animated.View>
)
}
}
const style = StyleSheet.create({
navWrap: {
flex: 1,
position: 'absolute',
width: '100%',
height: '100%',
top: 0,
right: 0,
zIndex: 10,
},
navMask: {
position: 'absolute',
top: 0,
right: 0,
width: '100%',
height: '100%',
backgroundColor: '#000',
opacity: 0.8,
},
menuWrap: {
position: 'absolute',
flex: 1,
width: 230,
height: '100%',
paddingTop: 0,
backgroundColor: 'white',
zIndex: 11,
borderWidth: 0,
borderColor: 'red',
borderTopWidth: 60,
borderTopColor: '#123357'
},
menuCloseIcon: {
fontSize:30,
fontWeight:'100',
color: '#979797'
},
});

React Native Image absolute positioning

I am trying to position two images at the bottom border, each side with the 50% width, so as to properly scale to any device size. But I can't seem to get any absolute positioning to behave in a reproducible way.
I've made an example Snack: https://snack.expo.io/rJd3OkVIM
The App component and the associated style is shown below:
import React, { Component } from 'react';
import { View, StyleSheet, Image } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Image
style={styles.img}
resizeMode="contain"
resizeMethod="resize"
source={require('./leftbg.png')}
/>
<Image
style={styles.imgr}
resizeMode="contain"
resizeMethod="resize"
source={require('./rightbg.png')}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
},
img: {
width: '50%',
position: 'absolute',
left: 0,
},
imgr: {
width: '50%',
position: 'absolute',
right: 0,
}
});
Each device sets the image vertical centering to a random part of the screen, every time the project is opened the centering seems to change.
To achieve your requirement, let wrap 2 images to a View with flexDirection: 'row', then make the top level View with justityContent: 'flex-end'.
I made a simple code with hard code height for 200 px and put the background as goldenrod for easy recognization.
If you need to keep the ratio of image, follow this answer: Maintain aspect ratio of image with full width in React Native
import React, { Component } from 'react';
import { View, StyleSheet, Image } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={{ flexDirection: 'row', height: 200, backgroundColor: 'goldenrod' }}>
<Image
style={styles.img}
source={require('./leftbg.png')}
/>
<Image
style={styles.imgr}
source={require('./rightbg.png')}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
justifyContent: 'flex-end',
},
img: {
width: '50%',
height: '100%',
resizeMode: 'cover',
},
imgr: {
width: '50%',
height: '100%',
resizeMode: 'cover',
}
});
You haven't given your images a vertical position reference ( top:x || bottom: x ), so this is probably why you are experiencing this behaviour.
Try setting bottom: 0 on img and imgr styles in order to set them to the bottom of the screen.