I want to make 2 variables for the main colors which can be used on the whole app. Here is the code:
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
const primary = '#27bcb5';
const secondary = '#ffffff;
How can I export these 2 variables together for using on the app components?
Issue with the solution is :
import {DefaultStyle} from './variables';
const screenHeight = Dimensions.get("window").height;
const styles = {
wrapper: {},
slide1: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: primary,
it says can't find variable primary
You can export them with:
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
const primary = '#27bcb5';
const secondary = '#ffffff';
export const DefaultStyle = {
primary,
secondary,
}
So you can use them as:
import { DefaultStyle } from './default-variables';
console.log(DefaultStyle.primary);
console.log(DefaultStyle.secondary);
You could do a named export. E.g. :
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
const primary = '#27bcb5';
const secondary = '#ffffff';
export { primary, secondary };
After that, you can import your styles via:
import { primary, secondary } from 'YOUR_FILE_PATH'
// Declarations of colors variables
const Maincolor ="#333640"
const Backgroundcolor = "#FFFFFF"
const Gridcolor = "#A7A9AB"
const colors = // Made to be bind in one because only one export is possiable
{
Maincolor,
Backgroundcolor,
Gridcolor,
}
export default colors; // exporting as default
// And in your main code where you have to include
import colors from 'path_of_the_file'
// then in styles use
style ={{backgroundColor:color.Maincolor}} // example how to use
I like to do something better, define a global file where you can save like Moment() time, Device Measures, etc.
global.js
const global = {
PRIMARY: '#27bcb5',
SECONDARY: '#ffffff,
DEVICE_MEASURES:{
WIDTH: Dimensions.get("window").width,
HEIGHT: Dimensions.get("window").height;
},
...
}
export default global
And then in your code:
import GLOBAL from '(directory)/global'
<View style={{ width: GLOBAL.DEVICE_MEASURES.WIDTH }}>
<Text style={{ color: GLOBAL.PRIMARY }}>
sup
</Text>
</View>
It's just better to have a global file where you can store all your app's configurations.
Related
I'm taking a React Native tutorial and I'm getting the error:
Failed to compile
Syntax Error
None of these files exist:
*..\..\..\..\..\.src\screens\ListScreen(.native|ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|tsx|ios.js|native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|native.json|.json)
*..\..\..\..\..\.src\screens\ListScreen\index(.native|ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|tsx|ios.js|native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|native.json|.json)
There are only three files being imported into my App.js file and the error shows when I import ListScreen from ".src/screens/ListScreen". When I comment out that line the error goes away. I have no idea what's causing it.
Below is the code from the four JS files and the folder structure. It's a course on Udemy and no one else has encountered it so I'm not sure what I'm doing wrong since all the other files were authored by the instructor which I downloaded at the beginning of the course.
App.js
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import HomeScreen from "./src/screens/HomeScreen";
import ComponentsScreen from "./src/screens/ComponentsScreen";
import ListScreen from ".src/screens/ListScreen";
const navigator = createStackNavigator(
{
Home: HomeScreen,
Components: ComponentsScreen,
},
{
initialRouteName: "Components",
defaultNavigationOptions: {
title: "App",
}
}
);
export default createAppContainer(navigator);
HomeScreen.js
import React from 'react';
import { Text, StyleSheet } from 'react-native';
const HomeScreen = () => {
return <Text style={styles.text}>ProjectWherewoof</Text>
};
const styles = StyleSheet.create({
text: {
fontSize: 30,
},
});
export default HomeScreen;
ComponentsScreen.js
import { Text, StyleSheet, View } from "react-native";
const ComponentsScreen = () => {
const greeting = <Text>Getting Started with React Native</Text>;
const myname = 'Arturo';
return (
<View>
<Text style={styles.greetingStyle}>{greeting}</Text>
<Text style={styles.nameStyle}>My name is {myname} !!!</Text>
</View>
);
};
const styles = StyleSheet.create({
greetingStyle: {
fontSize: 45,
},
nameStyle: {
fontSize: 20,
}
});
export default ComponentsScreen;
ListScreen.js
import React from 'react';
import { Text, StyleSheet } from 'react-native';
const ListScreen = () => {
return <Text>List Screen</Text>;
};
const styles = StyleSheet.create({});
export default ListScreen;
Folder Structure:
Folder Structure snapshot
You're missing a slash in import ListScreen from ".src/screens/ListScreen";. .src is not a directory in your project.
It should be import ListScreen from "./src/screens/ListScreen"; (or just "src/screens/ListScreen";)
I am trying to apply a 'flexDirection' property of 'row' to my layout, but the output is different depending on the method used. I want to understand why react native is behaving in such a way.
I've tried playing around with different properties to achieve the required result.
Keypad.js
import React from 'react';
import { View } from 'react-native';
import Keys from './Keys';
import styles from './styles';
let rows = [];
let nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
row.push(<Keys number={nums[i][j]} />);
}
rows.push(<View style={"this is where the problem occurs"}>{row}</View>);
}
const Keypad = () => <View>{rows}</View>;
export default Keypad;
Keys.js
import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
import styles from './styles';
const Keys = ({ number }) => (
<TouchableOpacity style={styles.button}>
<Text style={styles.number}>{number}</Text>
</TouchableOpacity>
);
export default Keys;
styles.js
import EStyleSheet from 'react-native-extended-stylesheet';
export default EStyleSheet.create({
button: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
number: {
fontSize: 40
},
row: {
flexDirection: 'row'
}
});
index.js
import React from 'react';
import EStyleSheet from 'react-native-extended-stylesheet';
import TestScreen from './screens/TestScreen';
EStyleSheet.build({
// WelcomePage
$white: '#FFFFFF',
$black: '#000000',
$getStartedBlue: '#40A5D6',
$welcomeMessage: '#767676',
// VerificationPage
$primaryBackground: '#C3C3C3',
$secondBackground: '#40A5D6',
$numbers: '#352641',
$backspace: '#9599B3',
$nextButton: '#0062CC'
// $outline: 1
});
export default () => <TestScreen />;
App.js
import App from './app/index';
export default App;
I am expecting numbers in the form of a keypad. I've marked the line in Keypad.js where the problem lies.
When I use:
rows.push(<View style={{ flexDirection: 'row' }}>{row}</View>);
as the styling property I get the required output (a keypad from numbers 1-9).
But when I use:
rows.push(<View style={styles.row}>{row}</View>);
then all the numbers overlap over one another and appear as a smudge on the screen.
In my understanding, it is just another way to apply the same property, so why is the result different?
You probably missing the important part from the docs,
In app entry point call EStyleSheet.build() to actually calculate styles:
You need to add this in app.js file,
/* app.js */
import EStyleSheet from 'react-native-extended-stylesheet';
EStyleSheet.build({ // always call EStyleSheet.build() even if you don't use global variables!
$textColor: '#0275d8'
});
I am trying to setup global styles in react-native.
I have imported
import {injectGlobal} from 'styled-components';
and have
class XoxoContainer extends Component {
render() {
return <Xoxo {...this.props} />
}
}
injectGlobal`
font-family: '20'
`;
But I keep getting styledComponents.injectGlobals is not a function. in the console.
That function is not part of the library on react-native according to this Github issue. That's why it keeps saying that it's not a function, because it can't find it.
create a styles.js file like this:
import React, {Component} from 'react';
import {
Platform,
StyleSheet
} from 'react-native';
export const styles = StyleSheet.create({
view_flex_one_white: {
flex: 1,
backgroundColor: white
}});
and use this anywhere in your app with import
import {styles} from "...link to file/styles";
render(){
return(
<View style={styles.view_flex_one_white}>
</View>
)
}
Create a js file with this pattern:
'use strict';
var React = require('react-native');
var myStyle = React.StyleSheet.create({
style1: { },
style2: { }
)}
module.exports = myStyle;
your component js use require to use that style sheet
var customStyle = require('../the/path/to/commonStyleSheet');
Use now like this:
<View style = {customStyle .style1} />
I am working on a react-native app created using: create-react-native-app
As far as I can tell the most top level component is inside the App.js file and I have imported the Provider there and wrapped it around the Top Level Component but I am still getting the following errors for some reason:
ExceptionsManager.js:65 Invariant Violation: Could not find "store" in
either the context or props of "Connect(App)". Either wrap the root
component in a , or explicitly pass "store" as a prop to
"Connect(App)".
What am I doing wrong?
Here is my code:
import React from 'react';
import { StyleSheet, Text, View, FlatList, TextInput, StatusBar, Button,TouchableOpacity } from 'react-native';
import { TabNavigator, StackNavigator } from 'react-navigation';
import { Constants } from 'expo'
import { purple, white } from './utils/colors'
import { saveDeckTitle, getDecks, getDeck, addCardToDeck } from './utils/api'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import reducer from './reducers'
import { connect } from 'react-redux'
import Decks from './components/Decks'
import NewQuestionView from './components/NewQuestionView'
import NewDeck from './components/NewDeck'
const R = require('ramda')
const store = createStore(reducer)
const DecksETC = StackNavigator({
Decks: {
screen: Decks
},
NewDeck: {
screen: NewDeck
},
NewQuestionView: {
screen: NewQuestionView
}
})
const NewDeckETC = StackNavigator({
NewDeck: {
screen: NewDeck
},
Decks: {
screen: Decks
},
NewQuestionView: {
screen: NewQuestionView
}
})
const Tabs = TabNavigator({
DecksETC: {
screen: DecksETC
},
NewDeckETC: {
screen: NewDeckETC
}
});
class App extends React.Component {
render() {
console.log('R', R)
return (
<Provider store={store}>
<Tabs />
</Provider>
// <Tabs />
);
}
}
const styles = StyleSheet.create({
container: {
paddingTop: 23,
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
input: {
margin: 15,
height: 40,
borderColor: '#7a42f4',
borderWidth: 1
},
});
function mapStateToProps(state) {
console.log('mapStateToProps')
debugger
return {
'sample': 'sample'
}
}
export default connect(mapStateToProps)(App)
The problem is that App component does not know anything about the store because the Provider component is what brings the Redux store into its children components. The App component itself does not receive a reference to the store, so when you try to connect, the store is not found.
Well I see you have use connect function for the root component directly, which is a pattern I never see before. Let's try the normal way, which is that you will create a Root component and just use it inside Provider. Then you will pass child components into that Root component.
You will then separate each child component into a new file. In each file, you will use connect() to pass redux store into that component. That's the common pattern I see in many many projects. And this pattern will help you avoid confusing situation like above!
Well the function connect requires two functions mapStateToProps and mapDispatchToProps as arguments
i.e. export default connect(mapStateToProps,mapDispatchToProps)(App)
if you do not have a mapDispatchToProps then simply pass null.
i.e. export default connect(mapStateToProps,null)(App)
I have a file that I am using to define global styles. I would like to define a const within the file to use for the underlineColorAndroid prop.
My global stylesheet looks like this:
const React = require('react-native')
const { StyleSheet } = React
const underlineColorAndroid = '#F86C51'
module.exports = StyleSheet.create({
background: {
backgroundColor: '#F5FCFF',
},
})
I import the file like this:
import globalStyles from '../styles/global'
And I use the style within the prop like this:
underlineColorAndroid={globalStyles.underlineColorAndroid}
This doesn't work, any ideas how I can do this?
Instead of using the module.exports syntax, you can use the ES6 export statements to define both default and named exports:
export const underlineColorAndroid = '#F86C51'
export default StyleSheet.create({
background: {
backgroundColor: '#F5FCFF',
},
})
The default export can be imported as before:
import globalStyles from '../styles/global';
The named exports can be imported using curly braces to denote destructuring:
import { underlineColorAndroid } from '../styles/global';
Or both at the same time:
import globalStyles, { underlineColorAndroid } from '../styles/global';
However, it might be better to split the style sheet and the named variables into different files for clarity.