ditox-react 3.0.1 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,14 +2,14 @@
2
2
 
3
3
  <img alt="lemon" src="https://raw.githubusercontent.com/mnasyrov/ditox/master/lemon.svg" width="120" />
4
4
 
5
- **Dependency injection container for React.js**
5
+ **Dependency injection helpers for React.js**
6
6
 
7
7
  Please see the documentation at [ditox.js.org](https://ditox.js.org)
8
8
 
9
- [![npm](https://img.shields.io/npm/v/ditox)](https://www.npmjs.com/package/ditox)
9
+ [![npm](https://img.shields.io/npm/v/ditox-react)](https://www.npmjs.com/package/ditox-react)
10
10
  [![stars](https://img.shields.io/github/stars/mnasyrov/ditox)](https://github.com/mnasyrov/ditox/stargazers)
11
- [![types](https://img.shields.io/npm/types/ditox)](https://www.npmjs.com/package/ditox)
12
- [![licence](https://img.shields.io/github/license/mnasyrov/ditox)](https://github.com/mnasyrov/ditox/blob/master/LICENSE)
11
+ [![types](https://img.shields.io/npm/types/ditox-react)](https://www.npmjs.com/package/ditox-react)
12
+ [![license](https://img.shields.io/github/license/mnasyrov/ditox)](https://github.com/mnasyrov/ditox/blob/master/LICENSE)
13
13
  [![coverage](https://coveralls.io/repos/github/mnasyrov/ditox/badge)](https://coveralls.io/github/mnasyrov/ditox)
14
14
 
15
15
  ## Installation
@@ -20,14 +20,14 @@ You can use the following command to install this package:
20
20
  npm install --save ditox-react
21
21
  ```
22
22
 
23
- The packages can be used as [UMD](https://github.com/umdjs/umd) modules. Use
24
- [jsdelivr.com](https://jsdelivr.com) CDN site to load
23
+ Packages can be used as [UMD](https://github.com/umdjs/umd) modules. Use
24
+ [jsdelivr.com](https://jsdelivr.com) CDN to load
25
25
  [ditox](https://www.jsdelivr.com/package/npm/ditox) and
26
26
  [ditox-react](https://www.jsdelivr.com/package/npm/ditox-react):
27
27
 
28
28
  ```html
29
- <script src="//cdn.jsdelivr.net/npm/ditox/dist/umd/index.js" />
30
- <script src="//cdn.jsdelivr.net/npm/ditox-react@2.3.0/dist/umd/index.js" />
29
+ <script src="//cdn.jsdelivr.net/npm/ditox/dist/umd/index.js"></script>
30
+ <script src="//cdn.jsdelivr.net/npm/ditox-react/dist/umd/index.js"></script>
31
31
  <script>
32
32
  const container = Ditox.createContainer();
33
33
  // DitoxReact.useDependency(SOME_TOKEN);
@@ -40,20 +40,24 @@ The packages can be used as [UMD](https://github.com/umdjs/umd) modules. Use
40
40
  in React apps:
41
41
 
42
42
  - Components:
43
- - `DepencencyContainer` - provides a new or existed container to React
44
- components.
45
- - `DepencencyModule` - binds a dependency module to a new container.
46
- - `CustomDepencencyContainer` - provides an existed dependency container.
43
+ - `DependencyContainer` — creates and provides a new container to React
44
+ children. Optionally binds dependencies via a binder callback. Can start a
45
+ new root container with `root` flag.
46
+ - `DependencyModule` — binds a dependency module to a new container (supports
47
+ `scope`: `singleton` (default) or `scoped`).
48
+ - `CustomDependencyContainer` — provides an existing dependency container.
47
49
  - Hooks:
48
- - `useDependencyContainer()` - returns a provided dependency container.
49
- - `useDependency()` - returns a resolved value by a specified token. It throws
50
- an error in case a container or value is not found.
51
- - `useOptionalDependency()` - returns a resolved value by a specified token,
52
- or returns `undefined` in case a container or value is not found.
50
+ - `useDependencyContainer(mode?)` — returns a provided dependency container.
51
+ With `mode = 'strict'` it throws if the container is not provided; by
52
+ default it returns `undefined` when not provided.
53
+ - `useDependency(token)` — resolves and returns a value by a specified token.
54
+ Throws if a container or the value is not found.
55
+ - `useOptionalDependency(token)` — returns a resolved value by a specified
56
+ token, or `undefined` if not found or not provided.
53
57
 
54
58
  ## Usage Examples
55
59
 
56
- ```jsx
60
+ ```tsx
57
61
  import {token} from 'ditox';
58
62
  import {
59
63
  DependencyContainer,
@@ -62,8 +66,8 @@ import {
62
66
  useOptionalDependency,
63
67
  } from 'ditox-react';
64
68
 
65
- const FOO_TOKEN = token();
66
- const BAR_TOKEN = token();
69
+ const FOO_TOKEN = token<string>();
70
+ const BAR_TOKEN = token<string>();
67
71
 
68
72
  function appDependencyBinder(container) {
69
73
  container.bindValue(FOO_TOKEN, 'foo');
@@ -71,15 +75,15 @@ function appDependencyBinder(container) {
71
75
 
72
76
  function App() {
73
77
  return (
74
- <DependencyContainer binder={appDependencyBinder}>
78
+ <DependencyContainer root binder={appDependencyBinder}>
75
79
  <NestedComponent />
76
80
  </DependencyContainer>
77
81
  );
78
82
  }
79
83
 
80
84
  function NestedComponent() {
81
- // Get access to the container
82
- const container = useDependencyContainer();
85
+ // Get access to the container (optionally)
86
+ const container = useDependencyContainer(); // or useDependencyContainer('strict') to enforce presence
83
87
 
84
88
  // Use a resolved value
85
89
  const foo = useDependency(FOO_TOKEN);
@@ -95,21 +99,85 @@ function NestedComponent() {
95
99
  }
96
100
  ```
97
101
 
102
+ ### Providing an existing container
103
+
104
+ ```tsx
105
+ import {createContainer, token} from 'ditox';
106
+ import {CustomDependencyContainer, useDependency} from 'ditox-react';
107
+
108
+ const GREETING_TOKEN = token<string>();
109
+
110
+ const container = createContainer();
111
+ container.bindValue(GREETING_TOKEN, 'Hello');
112
+
113
+ function App() {
114
+ return (
115
+ <CustomDependencyContainer container={container}>
116
+ <Greeting />
117
+ </CustomDependencyContainer>
118
+ );
119
+ }
120
+
121
+ function Greeting() {
122
+ const text = useDependency(GREETING_TOKEN);
123
+ return <>{text}</>;
124
+ }
125
+ ```
126
+
98
127
  ## Dependency Modules
99
128
 
100
- Dependency modules can be provided to the app with `<DependencyModule />`
101
- component:
129
+ Dependency modules can be provided to the app with the `<DependencyModule />`
130
+ component. You can also specify a binding scope (`singleton` by default or
131
+ `scoped`):
102
132
 
103
133
  ```tsx
104
134
  function App() {
105
135
  return (
106
- <DependencyModule module={LOGGER_MODULE}>
136
+ <DependencyModule module={LOGGER_MODULE} scope="singleton">
107
137
  <NestedComponent />
108
138
  </DependencyModule>
109
139
  );
110
140
  }
111
141
  ```
112
142
 
143
+ ## API Reference
144
+
145
+ - Components
146
+
147
+ - DependencyContainer(props)
148
+ - props.children: ReactNode — React children to wrap
149
+ - props.root?: boolean — if true, create a new root container that does not
150
+ inherit from any parent container
151
+ - props.binder?: (container: Container) => unknown — optional callback to
152
+ set up bindings for the newly created container
153
+ - Behavior: creates a new container linked to the parent container by
154
+ default; removes all bindings on unmount
155
+ - CustomDependencyContainer(props)
156
+ - props.children: ReactNode — React children to wrap
157
+ - props.container: Container — an existing container instance to provide
158
+ - DependencyModule(props)
159
+ - props.children: ReactNode — React children to wrap
160
+ - props.module: ModuleDeclaration — module to bind to a new container
161
+ - props.scope?: 'singleton' | 'scoped' — scope for the module bindings
162
+ (default: 'singleton')
163
+
164
+ - Hooks
165
+ - useDependencyContainer(mode?: 'optional' | 'strict')
166
+ - Returns the container. In 'strict' mode throws if not provided; in
167
+ optional mode returns undefined if not provided
168
+ - useDependency<T>(token: Token<T>): T
169
+ - Resolves and returns the value; throws if not bound or container is
170
+ missing
171
+ - useOptionalDependency<T>(token: Token<T>): T | undefined
172
+ - Returns the value if bound, otherwise undefined
173
+
174
+ Notes:
175
+
176
+ - A parent container (if provided higher in the tree) is used as the parent for
177
+ new containers created by DependencyContainer and DependencyModule, unless
178
+ root is set to true.
179
+ - All bindings made inside a DependencyContainer are removed on unmount.
180
+
113
181
  ---
114
182
 
115
183
  This project is licensed under the
@@ -6,8 +6,8 @@ import { ReactNode, ReactElement } from 'react';
6
6
  */
7
7
  type DependencyContainerBinder = (container: Container) => unknown;
8
8
  /**
9
- * Specifies an existed container or options for a new container:
10
- * @property binder - A callback which setup bindings to the container.
9
+ * Specifies options for a new container:
10
+ * @property binder - A callback which sets up bindings for the container.
11
11
  * @property root - If `true` then a new container does not depend on any parent containers
12
12
  */
13
13
  type DependencyContainerParams = {
@@ -18,16 +18,16 @@ type DependencyContainerParams = {
18
18
  /**
19
19
  * Provides a new dependency container to React app
20
20
  *
21
- * This component creates a new container and provides it down to React children.
21
+ * This component creates a new container and provides it to React children.
22
22
  *
23
- * If `binder` callback is specified, it will be called for a new container
24
- * to binds it with dependencies.
23
+ * If a `binder` callback is specified, it will be called for the new container
24
+ * to bind it with dependencies.
25
25
  *
26
- * If a parent container is exist, it is connected to the current one by default.
27
- * For making a new root container specify `root` parameter as `true`,
26
+ * If a parent container exists, it is connected to the current one by default.
27
+ * To create a root container specify `root` parameter as `true`,
28
28
  * and the container will not depend on any parent container.
29
29
  *
30
- * @param params.binder - A callback which setup bindings to the container.
30
+ * @param params.binder - A callback which sets up bindings for the container.
31
31
  * @param params.root - If `true` then a new container does not depend on any parent containers
32
32
  *
33
33
  * @example
@@ -101,7 +101,7 @@ declare function useOptionalDependency<T>(token: Token<T>): T | undefined;
101
101
  /**
102
102
  * Binds the module to a new dependency container.
103
103
  *
104
- * If a parent container is exist, it is connected to the current one by default.
104
+ * If a parent container exists, it is connected to the current one by default.
105
105
  *
106
106
  * @param params.module - Module declaration for binding
107
107
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
@@ -6,8 +6,8 @@ export declare const DependencyContainerContext: React.Context<Container | undef
6
6
  */
7
7
  export type DependencyContainerBinder = (container: Container) => unknown;
8
8
  /**
9
- * Specifies an existed container or options for a new container:
10
- * @property binder - A callback which setup bindings to the container.
9
+ * Specifies options for a new container:
10
+ * @property binder - A callback which sets up bindings for the container.
11
11
  * @property root - If `true` then a new container does not depend on any parent containers
12
12
  */
13
13
  export type DependencyContainerParams = {
@@ -18,16 +18,16 @@ export type DependencyContainerParams = {
18
18
  /**
19
19
  * Provides a new dependency container to React app
20
20
  *
21
- * This component creates a new container and provides it down to React children.
21
+ * This component creates a new container and provides it to React children.
22
22
  *
23
- * If `binder` callback is specified, it will be called for a new container
24
- * to binds it with dependencies.
23
+ * If a `binder` callback is specified, it will be called for the new container
24
+ * to bind it with dependencies.
25
25
  *
26
- * If a parent container is exist, it is connected to the current one by default.
27
- * For making a new root container specify `root` parameter as `true`,
26
+ * If a parent container exists, it is connected to the current one by default.
27
+ * To create a root container specify `root` parameter as `true`,
28
28
  * and the container will not depend on any parent container.
29
29
  *
30
- * @param params.binder - A callback which setup bindings to the container.
30
+ * @param params.binder - A callback which sets up bindings for the container.
31
31
  * @param params.root - If `true` then a new container does not depend on any parent containers
32
32
  *
33
33
  * @example
@@ -30,16 +30,16 @@ exports.DependencyContainerContext = (0, react_1.createContext)(undefined);
30
30
  /**
31
31
  * Provides a new dependency container to React app
32
32
  *
33
- * This component creates a new container and provides it down to React children.
33
+ * This component creates a new container and provides it to React children.
34
34
  *
35
- * If `binder` callback is specified, it will be called for a new container
36
- * to binds it with dependencies.
35
+ * If a `binder` callback is specified, it will be called for the new container
36
+ * to bind it with dependencies.
37
37
  *
38
- * If a parent container is exist, it is connected to the current one by default.
39
- * For making a new root container specify `root` parameter as `true`,
38
+ * If a parent container exists, it is connected to the current one by default.
39
+ * To create a root container specify `root` parameter as `true`,
40
40
  * and the container will not depend on any parent container.
41
41
  *
42
- * @param params.binder - A callback which setup bindings to the container.
42
+ * @param params.binder - A callback which sets up bindings for the container.
43
43
  * @param params.root - If `true` then a new container does not depend on any parent containers
44
44
  *
45
45
  * @example
@@ -3,7 +3,7 @@ import { ReactElement, ReactNode } from 'react';
3
3
  /**
4
4
  * Binds the module to a new dependency container.
5
5
  *
6
- * If a parent container is exist, it is connected to the current one by default.
6
+ * If a parent container exists, it is connected to the current one by default.
7
7
  *
8
8
  * @param params.module - Module declaration for binding
9
9
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
@@ -30,7 +30,7 @@ const DependencyContainer_1 = require("./DependencyContainer");
30
30
  /**
31
31
  * Binds the module to a new dependency container.
32
32
  *
33
- * If a parent container is exist, it is connected to the current one by default.
33
+ * If a parent container exists, it is connected to the current one by default.
34
34
  *
35
35
  * @param params.module - Module declaration for binding
36
36
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
@@ -6,8 +6,8 @@ export declare const DependencyContainerContext: React.Context<Container | undef
6
6
  */
7
7
  export type DependencyContainerBinder = (container: Container) => unknown;
8
8
  /**
9
- * Specifies an existed container or options for a new container:
10
- * @property binder - A callback which setup bindings to the container.
9
+ * Specifies options for a new container:
10
+ * @property binder - A callback which sets up bindings for the container.
11
11
  * @property root - If `true` then a new container does not depend on any parent containers
12
12
  */
13
13
  export type DependencyContainerParams = {
@@ -18,16 +18,16 @@ export type DependencyContainerParams = {
18
18
  /**
19
19
  * Provides a new dependency container to React app
20
20
  *
21
- * This component creates a new container and provides it down to React children.
21
+ * This component creates a new container and provides it to React children.
22
22
  *
23
- * If `binder` callback is specified, it will be called for a new container
24
- * to binds it with dependencies.
23
+ * If a `binder` callback is specified, it will be called for the new container
24
+ * to bind it with dependencies.
25
25
  *
26
- * If a parent container is exist, it is connected to the current one by default.
27
- * For making a new root container specify `root` parameter as `true`,
26
+ * If a parent container exists, it is connected to the current one by default.
27
+ * To create a root container specify `root` parameter as `true`,
28
28
  * and the container will not depend on any parent container.
29
29
  *
30
- * @param params.binder - A callback which setup bindings to the container.
30
+ * @param params.binder - A callback which sets up bindings for the container.
31
31
  * @param params.root - If `true` then a new container does not depend on any parent containers
32
32
  *
33
33
  * @example
@@ -4,16 +4,16 @@ export const DependencyContainerContext = createContext(undefined);
4
4
  /**
5
5
  * Provides a new dependency container to React app
6
6
  *
7
- * This component creates a new container and provides it down to React children.
7
+ * This component creates a new container and provides it to React children.
8
8
  *
9
- * If `binder` callback is specified, it will be called for a new container
10
- * to binds it with dependencies.
9
+ * If a `binder` callback is specified, it will be called for the new container
10
+ * to bind it with dependencies.
11
11
  *
12
- * If a parent container is exist, it is connected to the current one by default.
13
- * For making a new root container specify `root` parameter as `true`,
12
+ * If a parent container exists, it is connected to the current one by default.
13
+ * To create a root container specify `root` parameter as `true`,
14
14
  * and the container will not depend on any parent container.
15
15
  *
16
- * @param params.binder - A callback which setup bindings to the container.
16
+ * @param params.binder - A callback which sets up bindings for the container.
17
17
  * @param params.root - If `true` then a new container does not depend on any parent containers
18
18
  *
19
19
  * @example
@@ -3,7 +3,7 @@ import { ReactElement, ReactNode } from 'react';
3
3
  /**
4
4
  * Binds the module to a new dependency container.
5
5
  *
6
- * If a parent container is exist, it is connected to the current one by default.
6
+ * If a parent container exists, it is connected to the current one by default.
7
7
  *
8
8
  * @param params.module - Module declaration for binding
9
9
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
@@ -4,7 +4,7 @@ import { DependencyContainer, } from './DependencyContainer';
4
4
  /**
5
5
  * Binds the module to a new dependency container.
6
6
  *
7
- * If a parent container is exist, it is connected to the current one by default.
7
+ * If a parent container exists, it is connected to the current one by default.
8
8
  *
9
9
  * @param params.module - Module declaration for binding
10
10
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
@@ -6,8 +6,8 @@ import { ReactNode, ReactElement } from 'react';
6
6
  */
7
7
  type DependencyContainerBinder = (container: Container) => unknown;
8
8
  /**
9
- * Specifies an existed container or options for a new container:
10
- * @property binder - A callback which setup bindings to the container.
9
+ * Specifies options for a new container:
10
+ * @property binder - A callback which sets up bindings for the container.
11
11
  * @property root - If `true` then a new container does not depend on any parent containers
12
12
  */
13
13
  type DependencyContainerParams = {
@@ -18,16 +18,16 @@ type DependencyContainerParams = {
18
18
  /**
19
19
  * Provides a new dependency container to React app
20
20
  *
21
- * This component creates a new container and provides it down to React children.
21
+ * This component creates a new container and provides it to React children.
22
22
  *
23
- * If `binder` callback is specified, it will be called for a new container
24
- * to binds it with dependencies.
23
+ * If a `binder` callback is specified, it will be called for the new container
24
+ * to bind it with dependencies.
25
25
  *
26
- * If a parent container is exist, it is connected to the current one by default.
27
- * For making a new root container specify `root` parameter as `true`,
26
+ * If a parent container exists, it is connected to the current one by default.
27
+ * To create a root container specify `root` parameter as `true`,
28
28
  * and the container will not depend on any parent container.
29
29
  *
30
- * @param params.binder - A callback which setup bindings to the container.
30
+ * @param params.binder - A callback which sets up bindings for the container.
31
31
  * @param params.root - If `true` then a new container does not depend on any parent containers
32
32
  *
33
33
  * @example
@@ -101,7 +101,7 @@ declare function useOptionalDependency<T>(token: Token<T>): T | undefined;
101
101
  /**
102
102
  * Binds the module to a new dependency container.
103
103
  *
104
- * If a parent container is exist, it is connected to the current one by default.
104
+ * If a parent container exists, it is connected to the current one by default.
105
105
  *
106
106
  * @param params.module - Module declaration for binding
107
107
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ditox-react",
3
- "version": "3.0.1",
3
+ "version": "3.2.0",
4
4
  "description": "Dependency injection container for React.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -57,7 +57,7 @@
57
57
  "typedoc": "typedoc"
58
58
  },
59
59
  "dependencies": {
60
- "ditox": "3.0.0"
60
+ "ditox": "3.2.0"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
@@ -69,5 +69,5 @@
69
69
  "react-dom": "19.0.0",
70
70
  "react-test-renderer": "19.0.0"
71
71
  },
72
- "gitHead": "d5f20401897dc859670feeb0e55d74d4b54818db"
72
+ "gitHead": "493e9ba9fcb36feea88e3948a23a4249a119d03e"
73
73
  }
@@ -19,8 +19,8 @@ export const DependencyContainerContext = createContext<Container | undefined>(
19
19
  export type DependencyContainerBinder = (container: Container) => unknown;
20
20
 
21
21
  /**
22
- * Specifies an existed container or options for a new container:
23
- * @property binder - A callback which setup bindings to the container.
22
+ * Specifies options for a new container:
23
+ * @property binder - A callback which sets up bindings for the container.
24
24
  * @property root - If `true` then a new container does not depend on any parent containers
25
25
  */
26
26
  export type DependencyContainerParams = {
@@ -32,16 +32,16 @@ export type DependencyContainerParams = {
32
32
  /**
33
33
  * Provides a new dependency container to React app
34
34
  *
35
- * This component creates a new container and provides it down to React children.
35
+ * This component creates a new container and provides it to React children.
36
36
  *
37
- * If `binder` callback is specified, it will be called for a new container
38
- * to binds it with dependencies.
37
+ * If a `binder` callback is specified, it will be called for the new container
38
+ * to bind it with dependencies.
39
39
  *
40
- * If a parent container is exist, it is connected to the current one by default.
41
- * For making a new root container specify `root` parameter as `true`,
40
+ * If a parent container exists, it is connected to the current one by default.
41
+ * To create a root container specify `root` parameter as `true`,
42
42
  * and the container will not depend on any parent container.
43
43
  *
44
- * @param params.binder - A callback which setup bindings to the container.
44
+ * @param params.binder - A callback which sets up bindings for the container.
45
45
  * @param params.root - If `true` then a new container does not depend on any parent containers
46
46
  *
47
47
  * @example
@@ -9,7 +9,7 @@ import {
9
9
  /**
10
10
  * Binds the module to a new dependency container.
11
11
  *
12
- * If a parent container is exist, it is connected to the current one by default.
12
+ * If a parent container exists, it is connected to the current one by default.
13
13
  *
14
14
  * @param params.module - Module declaration for binding
15
15
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.