Reputation: 1389
I'm trying to store all the CSS style values into one single ts
file: styles/base.ts
, and export those values in styles/index.ts
file.
But when I try to use import the value from index.ts
in my App.tsx
file, it says
Unable to resolve module "styles/index" from "App.tsx": "styles/index" could not be found within the project.`.
I'm not sure is it a correct way to manage the styles inside application. Can anyone help me?
Here is how my base.ts
file looks like:
export const colors = {
grey: "#E3E1D6",
black: "#333333",
};
index.ts:
import * as BaseStyle from "./base";
export {BaseStyle};
App.tsx:
import React from "react";
import {StyleSheet, View, Text} from "react-native";
import {BaseStyle} from "styles/index";
const App = () => {
return (
<>
<View style={styles.body}>
<Text>Test</Text>
</View>
</>
);
};
const styles = StyleSheet.create({
body: {
backgroundColor: BaseStyle.colors.grey,
},
});
export default App;
And here is my package.json looks like:
{
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"react": "16.9.0",
"react-native": "0.61.4"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2",
"@react-native-community/eslint-config": "^0.0.5",
"@types/jest": "^24.0.18",
"@types/react-native": "^0.60.22",
"@types/react-test-renderer": "16.9.0",
"babel-jest": "^24.9.0",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.9.0",
"typescript": "^3.6.3"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
Upvotes: 0
Views: 3016
Reputation: 1304
You can create color class using this pattern
class Colors {
static transparents = "#ffffff";
static black = "#000";
}
export default Colors;
and you can import the color using this line
import Colors from './Colors';
Please follow this steps, this will help you its help me.
Upvotes: 1
Reputation: 529
I think it's only a typo mistake in your import. You should write:
import {BaseStyle} from "./styles/index";
Upvotes: 1