├── README.md
├── index.d.ts
├── package.json
├── setDefaultProps.js
└── tsconfig.json
/README.md:
--------------------------------------------------------------------------------
1 | # react-native-simple-default-props #
2 | Simplest way to set default props of any react-native components even Custom Component
3 |
4 | ### Motivation ###
5 | react-native-global-props has not update since Oct 2018
6 | It's useful but not work in some cases and import every single function for each component are not smart
7 | So I used my own version
8 |
9 | recently I thouhgt someone has same needs,
10 | I hope react-native-simple-default-props could help you
11 |
12 | ### Installation ###
13 | ```bash
14 | npm i react-native-simple-default-props
15 | or
16 | yarn add react-native-simple-default-props
17 | ```
18 |
19 | ### Usage ###
20 | ```js
21 | // import
22 | import setDefaultProps from 'react-native-simple-default-props'
23 |
24 | // use
25 | setDefaultProps(Component, props)
26 | ```
27 |
28 | ### Example ###
29 | ```js
30 | import React from 'react';
31 | import {Text, TextInput, SafeAreaView} from 'react-native';
32 | import setDefaultProps from 'react-native-simple-default-props'
33 |
34 | const defaultText = {
35 | style: [{color: 'orange'}, {fontSize: 30}],
36 | };
37 |
38 | // usage
39 | setDefaultProps(Text, defaultText);
40 | setDefaultProps(TextInput, {
41 | underlineColorAndroid: 'transparent',
42 | placeholder: 'this is placeholder',
43 | style: {
44 | fontSize: 30,
45 | padding: 0,
46 | },
47 | });
48 |
49 | // work with Custom Component also
50 | const CustomComponent = ({text = '', ...props}) => {
51 | return (
52 |
53 | {text}
54 |
55 | );
56 | };
57 |
58 | setDefaultProps(CustomComponent, {style: {backgroundColor: 'lightgreen'}});
59 |
60 | const App = () => {
61 | return (
62 |
63 | TEXT
64 |
65 |
66 |
67 |
68 | );
69 | };
70 |
71 | export default App;
72 | ```
73 |
74 | - Example Image
75 |
76 |
--------------------------------------------------------------------------------
/index.d.ts:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 |
3 | declare function setDefaultProps(Component:React.ComponentType, defaultProps: any): void
4 |
5 | export default setDefaultProps
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-simple-default-props",
3 | "types": "index.d.ts",
4 | "version": "1.0.0",
5 | "description": "Simplest way to set default props for react-native components",
6 | "main": "setDefaultProps.js",
7 | "peerDependencies": {
8 | "@types/react": "*",
9 | "react": "*",
10 | "react-native": "*"
11 | },
12 | "scripts": {
13 | "test": "echo \"Error: no test specified\" && exit 1"
14 | },
15 | "devDependencies": {
16 | "@types/react": "^16.8.0",
17 | "typescript": "^4.4.4"
18 | },
19 | "repository": {
20 | "type": "git",
21 | "url": "git+https://github.com/dioi2000/react-native-simple-default-props.git"
22 | },
23 | "keywords": [
24 | "react-native",
25 | "props",
26 | "default",
27 | "global",
28 | "style"
29 | ],
30 | "author": "dioi2000",
31 | "license": "MIT",
32 | "bugs": {
33 | "url": "https://github.com/dioi2000/react-native-simple-default-props/issues"
34 | },
35 | "homepage": "https://github.com/dioi2000/react-native-simple-default-props#readme"
36 | }
37 |
--------------------------------------------------------------------------------
/setDefaultProps.js:
--------------------------------------------------------------------------------
1 | const setDefaultProps = (Component, defaultProps) => {
2 | const componentRender = Component.render
3 | if(!componentRender) {
4 | Component.defaultProps = defaultProps
5 | return
6 | }
7 |
8 | Component.render = (props, ref) => {
9 | props = {
10 | ...defaultProps,
11 | ...props,
12 | style: [defaultProps.style, props.style],
13 | }
14 |
15 | return componentRender.call(this, props, ref)
16 | }
17 | }
18 |
19 | export default setDefaultProps
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es5",
4 | "module": "commonjs",
5 | "lib": ["es2015", "es2016", "esnext", "dom"],
6 | "allowJs": true,
7 | "jsx": "react-native",
8 | "noEmit": true,
9 | "declaration": true,
10 | "strict": true,
11 | "moduleResolution": "node",
12 | },
13 | }
--------------------------------------------------------------------------------