React Native : how to import flexbox styles from another file js? - react-native

I'm a newbie in React Native, and I try to learn basic flexbox from tutorial point but I always get an error, I have two file index.android.js and MyPresentationalComponent.js for styles code file.
This error picture, when I run myproject
index.android.js
import React, { Component } from 'react';
import {
AppRegistry,
styles,
View
} from 'react-native';
import MyPresentationalComponent from './MyPresentationalComponent';
export default class belajar extends Component {
constructor() {
super();
this.state = {
myText: 'Lorem ipsum dolor sit amet.'
};
}
render() {
return (
<View>
<MyPresentationalComponent style={styles.container} />
</View>
);
}
}
AppRegistry.registerComponent('belajar', () => belajar);
MyPresentationalComponent.js
import React, { Component } from 'react';
import {
View,
StyleSheet
} from 'react-native';
const MyPresentationalComponent = (props) => {
return (
<View style={styles.container}>
<View style={styles.redbox} />
<View style={styles.bluebox} />
<View style={styles.blackbox} />
</View>
);
};
export default MyPresentationalComponent;
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'grey',
height: 600
},
redbox: {
width: 100,
height: 100,
backgroundColor: 'red'
},
bluebox: {
width: 100,
height: 100,
backgroundColor: 'blue'
},
blackbox: {
width: 100,
height: 100,
backgroundColor: 'black'
},
});

The problem is that you are not exporting the styles variable out of the file, and so it will not be visible to other files, even when you import the class marked with export default. I would suggest the following:
import React, { Component } from 'react';
import {
View,
StyleSheet
} from 'react-native';
const MyPresentationalComponent = (props) => {
return (
<View style={styles.container}>
<View style={styles.redbox} />
<View style={styles.bluebox} />
<View style={styles.blackbox} />
</View>
);
};
export default MyPresentationalComponent;
export const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'grey',
height: 600
},
redbox: {
width: 100,
height: 100,
backgroundColor: 'red'
},
bluebox: {
width: 100,
height: 100,
backgroundColor: 'blue'
},
blackbox: {
width: 100,
height: 100,
backgroundColor: 'black'
},
});
Then, your index.android.js should look like this:
import React, { Component } from 'react';
import {
AppRegistry,
styles,
View
} from 'react-native';
import MyPresentationalComponent, {styles} from './MyPresentationalComponent';
export default class belajar extends Component {
constructor() {
super();
this.state = {
myText: 'Lorem ipsum dolor sit amet.'
};
}
render() {
return (
<View>
<MyPresentationalComponent style={styles.container} />
</View>
);
}
}
AppRegistry.registerComponent('belajar', () => belajar);
Please note the difference between defaut exports and named exports. You can have just one default export, and the name that you give it is not relevant at all, when you import it, it can use a different name. For named imports, you need to use the curly braces notation and the name must be the same in both export and import . However, you can have as many of them as you want.

According to #Gui Herzog answer simplier solution to include any external style file can be done:
stylesheet file
import {
StyleSheet
} from 'react-native';
export default styles = StyleSheet.create({
// or export const styles = StyleSheet.create({
Container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
})
Than simply use in your components:
import styles from './styles'
// or import { styles } from './styles'
using:
<View style={styles.Container}>

The problem is because you're exporting only the component MyPresentationalComponentand not its StyleSheet. If you want to have global styles I recommend you to create a different file. You could call it Styles.js and export it so you can use on all components you need. For example:
import { StyleSheet } from 'react-native'
module.exports = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'grey',
height: 600
},
})
Then you can import it on your MyPresentationalComponent and Index.js using:
import STYLES from 'Styles.js'
And to use you could:
<Component style={STYLES.container} />
Hope this helps!

Related

How to properly set the backgroundColor for a screen imported in React-native?

i still learning react-native and want to set the backgroundColor to an entire screen which is imported in app.js.
Even if i set the flex in 1, the height covering the screen but the width is always center but not covering the entire screen.
Here what i'm doing:
App.js
import { StatusBar } from "expo-status-bar";
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import Splash from "./Screen/Splash";
import TestScreen from "./Screen/TestScreen";
export default function App() {
return (
<View style={styles.container}>
{/* <Splash /> */}
<TestScreen />
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
// flexDirection: "column",
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
TestScreen.js:
import React from "react";
import { View, Text, StyleSheet } from "react-native";
const Test = () => {
return (
<View style={styles.container}>
<Text>Hello</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "rgb(0, 168, 150)",
},
});
export default Test;
So,how i can set properly the backgroundColor to ScreenTest to cover the entire screen?
Thanks
Within app.js maybe add the full-width property:
<TestScreen style = {styles.redbox} >
container: {
flex: 1
},
redbox: {
width: 100,
height: 100,
backgroundColor: 'red'
},

How to position a child component in react native

I am trying to position my bottomnavbar to the bottom of my screen, but is is not working?
I've been trying to set the position to absolute and then bottom: 0, but if I do it in the app.js component on or do it straight in the stylesheet of the bottomNavbar.js component, none of it works.
here is my app.js component:
import React, {Component} from 'react';
import {View, Text, StyleSheet} from 'react-native';
class BottomNavbar extends Component {
render() {
return (
<View style={styles.container}>
<Text>insights</Text>
<Text>Incidents</Text>
<Text>PLUS</Text>
<Text>Team Alerts</Text>
<Text>More</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'space-around',
backgroundColor: 'grey',
position: 'absolute',
bottom: 0,
},
});
export default BottomNavbar;
And here is my BottomNavbar component:
import React, {Component} from 'react';
import {View, Text, StyleSheet} from 'react-native';
class BottomNavbar extends Component {
render() {
return (
<View style={styles.container}>
<Text>insights</Text>
<Text>Incidents</Text>
<Text>PLUS</Text>
<Text>Team Alerts</Text>
<Text>More</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'space-around',
backgroundColor: 'grey',
position: 'absolute',
bottom: 0,
},
});
export default BottomNavbar;
Now I would like the bottomnavbar to show up at the bottom of the phonescreen, but I cannot figure out how to do that.
Try this one
import { AppRegistry } from 'react-native';
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
class BottomNavbar extends Component {
render() {
return (
<View style={styles.container}>
<Text>insights</Text>
<Text>Incidents</Text>
<Text>PLUS</Text>
<Text>Team Alerts</Text>
<Text>More</Text>
</View>
);
}
}
class App extends Component {
render() {
return (
<View style={styles.appContainer}>
{/* ...other components */}
<BottomNavbar />
</View>
);
}
}
const styles = StyleSheet.create({
appContainer: {
flex: 1,
},
container: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
backgroundColor: 'grey',
position: 'absolute',
bottom: 0,
width: '100%',
height: 80,
},
});
AppRegistry.registerComponent('myApp', () => App);

Image becoming transparent when placed within view

My app has a background image which has a slightly transparent view placed over it to provide a blue tint to the background image. I am now trying to place the app logo on top of these views but when I do so the logo appears to take on the opacity of the view providing the tint.
I am very new to react native but what I basically need is to have: background image > view > logo.
To demonstrate what I mean, the app logo should look like this:
But it currently looks washed out, like this:
App.js
import React, { Component } from 'react';
import { View, Text, ImageBackground } from 'react-native';
import Logo from './Logo';
class App extends Component {
render() {
return (
<ImageBackground
source={require('./images/city.png')}
style={styles.backgroundStyle}
>
<View style={styles.backgroundOverlayStyle}>
<Logo />
</View>
</ImageBackground>
);
}
}
const styles = {
backgroundStyle: {
flex: 1,
backgroundColor: '#000000',
width: '100%',
height: '100%'
},
backgroundOverlayStyle: {
flex: 1,
backgroundColor: '#003284',
opacity: 0.5
}
};
export default App;
Logo.js
import React from 'react';
import { View, Image } from 'react-native';
const Logo = () => {
const { logoStyle } = styles;
return (
<View style={styles.container}>
<Image style={logoStyle} source={require('./images/request-first-logo-white.png')} />
</View>
);
};
const styles = {
container: {
alignItems: 'center',
justifyContent: 'center',
opacity: 1,
},
logoStyle: {
width: '70%',
height: '65%',
resizeMode: 'contain',
//backgroundColor: 'blue'
}
};
export default Logo;
Thank you for any help.
you need you define blurRadius for ImageBackground
import React, { Component } from 'react';
import { View, Text, ImageBackground,Image } from 'react-native';
const Logo = () => {
const { logoStyle } = styles;
return (
<View style={styles.container}>
<Image style={logoStyle} source={{uri:'https://www.what-dog.net/Images/faces2/scroll001.jpg'}} />
</View>
);
};
export default class App extends Component {
render() {
return (
<ImageBackground
source={{uri : 'https://pngstocks.com/wp-content/uploads/2018/03/cb-background-10.jpeg'}}
style={styles.backgroundStyle}
blurRadius={30}
>
<Logo />
</ImageBackground>
);
}
}
const styles = {
backgroundStyle: {
flex: 1,
backgroundColor: '#000000',
width: '100%',
height: '100%'
},
backgroundOverlayStyle: {
flex: 1,
backgroundColor: '#003284',
},
container: {
alignItems: 'center',
justifyContent: 'center',
},
logoStyle: {
width: 100,
height: 100,
resizeMode: 'contain',
//backgroundColor: 'blue'
}
};

react native display image using api url

I want to display image in my react native program which is taken from nasa api. I created a react native project:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
StatusBar,
Image
} from 'react-native';
import api from './utilities/api';
export default class test extends Component {
constructor(props){
super(props);
this.state ={
apod: [],
apodName: ''
}
}
componentWillMount(){
api.getApod().then((res)=> {
this.setState({
apod: res.apod,
apodName: res.url
})
});
}
render() {
console.log("Apod:", this.state.hdurl);
return (
<View>
<StatusBar
backgroundColor="#00FA9A"
barStyle="dark-content"
/>
<Text style={styles.welcome}>
Apod: {this.state.apodName}
</Text>
<Image source={{uri: api.hdurl}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
thumbnail: {
width: 53,
height: 81,
}
});
I also have api.js file :
var api ={
getApod(){
var url = 'https://api.nasa.gov/planetary/apod?api_key=MY KEY';
return fetch(url).then((res) => res.json());
}
};
module.exports = api;
Result I have on screen, its just writes an image link. But how to display actual image

Different style files for different components

I'm learning react-native and have came up with a problem. I'm following a Tutorial series but might have missed something. I'm trying to use different style files for different components but the styles doesn't seem to apply.
Here is the snippet of the code:
index.ios.js
import React, {
AppRegistry,
Component,
Text,
View,
StatusBar
} from 'react-native';
StatusBar.setBarStyle('light-content');
class iTunesBrowser extends Component {
render() {
return (
<View style={styles.global.mainContainer} >
<View style={styles.navbar.appearance}>
<View style={styles.navbar.button} ></View>
<Text style={styles.navbar.title}>iTunesBrowser</Text>
<Text style={styles.navbar.button} >Search</Text>
</View>
<View style={styles.global.content}>
<Text> Some Text </Text>
</View>
</View>
);
}
}
import styles from './styles';
AppRegistry.registerComponent('iTunesBrowser', () => iTunesBrowser);
styles.js
export default {
global: require('./styles/global'),
navbar: require('./styles/navbar')
};
styles/global.js
import React, {StyleSheet} from 'react-native';
export default StyleSheet.create({
mainContainer: {
flex: 1
},
content: {
flex: 1,
backgroundColor: '#ccc'
}
});
styles/navbar.js
import React, {StyleSheet} from 'react-native';
export default StyleSheet.create({
appearance: {
backgroundColor: '#003333',
paddingTop: 30,
paddingBottom: 10,
flexDirection: 'row'
},
title: {
color: '#FFFFFF',
textAlign: 'center',
fontWeight: 'bold',
flex: 1
},
button: {
width: 50,
color: '#FEFEFE',
textAlign: 'center'
}
});
The styles work when put on the index or in a single style file.
I've tried your example and it works when you use JS files with this pattern:
import React, {StyleSheet} from 'react-native';
var global = StyleSheet.create({
mainContainer: {
flex: 1
},
content: {
flex: 1,
backgroundColor: '#ccc'
}
});
module.exports = global;
and
import React, {StyleSheet} from 'react-native';
var navbar = StyleSheet.create({
appearance: {
backgroundColor: '#003333',
paddingTop: 30,
paddingBottom: 10,
flexDirection: 'row'
},
title: {
color: '#FFFFFF',
textAlign: 'center',
fontWeight: 'bold',
flex: 1
},
button: {
width: 50,
color: '#FEFEFE',
textAlign: 'center'
}
});
module.exports = navbar;
The typical way to work with component styles in React Native is to keep the creation of your StyleSheets in the components themselves, not in separate files as you may be used to on the web with CSS. That way everything about the Component, the structure and the style are in the same file.
You can still have a global stylesheet, and pull in those styles when needed, but keep Component styles in the Component.
For a great example on how the community and Facebook recommends styling your components check out: http://makeitopen.com/