@tramvai/react 1.60.1 → 1.62.1
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 +20 -15
- package/package.json +2 -2
- package/README.en.md +0 -109
package/README.md
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: '@tramvai/react'
|
|
3
|
+
sidebar_position: 2
|
|
4
|
+
---
|
|
5
|
+
|
|
1
6
|
# React
|
|
2
7
|
|
|
3
|
-
`@tramvai/react` -
|
|
8
|
+
`@tramvai/react` - library for integrating tramvai features with `React` components
|
|
4
9
|
|
|
5
|
-
##
|
|
10
|
+
## Install
|
|
6
11
|
|
|
7
12
|
```bash
|
|
8
13
|
npm i --save @tramvai/react
|
|
@@ -10,7 +15,7 @@ npm i --save @tramvai/react
|
|
|
10
15
|
|
|
11
16
|
## DI
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
When creating components, you may need to get data from di, for this there is a hook `useDi` and HoC `withDi`
|
|
14
19
|
|
|
15
20
|
### useDi
|
|
16
21
|
|
|
@@ -22,15 +27,15 @@ type useDi = (deps: Record<string, string | Token>) => Record<string, any>;
|
|
|
22
27
|
type useDi = (dep: string | Token) => any;
|
|
23
28
|
```
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
A hook into which we can pass both an object with the required dependencies and an object with data will be returned to us, as well as a single token, where the result will be returned to us. When we call `useDi`, we get data from di and if we don't find data in di, an error will occur.
|
|
26
31
|
|
|
27
32
|
```javascript
|
|
28
33
|
import React from 'react';
|
|
29
34
|
import { useDi } from '@tramvai/react';
|
|
30
35
|
|
|
31
36
|
const MyComponent = () => {
|
|
32
|
-
const { logger } = useDi({ logger: 'logger' }); //
|
|
33
|
-
const Region = useDi(regionToken); //
|
|
37
|
+
const { logger } = useDi({ logger: 'logger' }); // pass the object
|
|
38
|
+
const Region = useDi(regionToken); // pass a single token
|
|
34
39
|
|
|
35
40
|
logger.info('text');
|
|
36
41
|
|
|
@@ -51,7 +56,7 @@ type withDi = (
|
|
|
51
56
|
) => (wrapper: React.ReactElement<any>) => React.ReactElement<any>;
|
|
52
57
|
```
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
A HoC that allows you to wrap any components, get data from `DI` and pass the result with dependencies to the props of the component
|
|
55
60
|
|
|
56
61
|
```javascript
|
|
57
62
|
import React from 'react';
|
|
@@ -72,31 +77,31 @@ class BoxyPage extends Component {
|
|
|
72
77
|
type useDiContainer = () => DI.Container;
|
|
73
78
|
```
|
|
74
79
|
|
|
75
|
-
|
|
80
|
+
Getting an instance of a DI container that has been added to the application context.
|
|
76
81
|
|
|
77
|
-
|
|
82
|
+
It is better not to use this hook, as it is very low-level and is intended for developing new hooks
|
|
78
83
|
|
|
79
84
|
## Error
|
|
80
85
|
|
|
81
|
-
|
|
86
|
+
To handle errors during rendering, React uses [Error Boundary](https://ru.reactjs.org/docs/error-boundaries.html#introducing-error-boundaries). This package provides its own version of Error Boundary which will log an error through a generic logger and display a stub for the wrapped component if an error occurs.
|
|
82
87
|
|
|
83
88
|
### ErrorBoundary
|
|
84
89
|
|
|
85
|
-
Error Boundary
|
|
90
|
+
Error Boundary component that monitors errors down the tree and, in case of a render error, will log an error and display the `fallbackComponent` component (passed as a props, by default it is a FallbackError from this package) instead of the fallen render subtree.
|
|
86
91
|
|
|
87
|
-
|
|
92
|
+
You can override the `fallbackComponent` through the `ERROR_BOUNDARY_FALLBACK_COMPONENT_TOKEN` provider.
|
|
88
93
|
|
|
89
94
|
### FallbackError
|
|
90
95
|
|
|
91
|
-
|
|
96
|
+
Component used by default as a stub for a subtree in which a render error occurred
|
|
92
97
|
|
|
93
98
|
### withError
|
|
94
99
|
|
|
95
|
-
|
|
100
|
+
Hook wrapping component in ErrorBoundary.
|
|
96
101
|
|
|
97
102
|
## lazy
|
|
98
103
|
|
|
99
|
-
|
|
104
|
+
To dynamically import components with SSR support, there is a high order `lazy` component:
|
|
100
105
|
|
|
101
106
|
```tsx
|
|
102
107
|
import { lazy } from '@tramvai/react';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.62.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/react.js",
|
|
6
6
|
"typings": "lib/react.d.ts",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@tinkoff/dippy": "0.7.38",
|
|
27
27
|
"@tinkoff/utils": "^2.1.2",
|
|
28
28
|
"@tinkoff/url": "0.7.37",
|
|
29
|
-
"@tramvai/core": "1.
|
|
29
|
+
"@tramvai/core": "1.62.1",
|
|
30
30
|
"react": ">=16.8.0",
|
|
31
31
|
"react-dom": ">=16.8.0",
|
|
32
32
|
"tslib": "^2.0.3"
|
package/README.en.md
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
# React
|
|
2
|
-
|
|
3
|
-
`@tramvai/react` - library for integrating tramvai features with `React` components
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm i --save @tramvai/react
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## DI
|
|
12
|
-
|
|
13
|
-
When creating components, you may need to get data from di, for this there is a hook `useDi` and HoC `withDi`
|
|
14
|
-
|
|
15
|
-
### useDi
|
|
16
|
-
|
|
17
|
-
```tsx
|
|
18
|
-
type useDi = (deps: Record<string, string | Token>) => Record<string, any>;
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
```tsx
|
|
22
|
-
type useDi = (dep: string | Token) => any;
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
A hook into which we can pass both an object with the required dependencies and an object with data will be returned to us, as well as a single token, where the result will be returned to us. When we call `useDi`, we get data from di and if we don't find data in di, an error will occur.
|
|
26
|
-
|
|
27
|
-
```javascript
|
|
28
|
-
import React from 'react';
|
|
29
|
-
import { useDi } from '@tramvai/react';
|
|
30
|
-
|
|
31
|
-
const MyComponent = () => {
|
|
32
|
-
const { logger } = useDi({ logger: 'logger' }); // pass the object
|
|
33
|
-
const Region = useDi(regionToken); // pass a single token
|
|
34
|
-
|
|
35
|
-
logger.info('text');
|
|
36
|
-
|
|
37
|
-
return (
|
|
38
|
-
<div>
|
|
39
|
-
Component
|
|
40
|
-
<Region />
|
|
41
|
-
</div>
|
|
42
|
-
);
|
|
43
|
-
};
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### withDi
|
|
47
|
-
|
|
48
|
-
```tsx
|
|
49
|
-
type withDi = (
|
|
50
|
-
deps: Record<string, string | Token>
|
|
51
|
-
) => (wrapper: React.ReactElement<any>) => React.ReactElement<any>;
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
A HoC that allows you to wrap any components, get data from `DI` and pass the result with dependencies to the props of the component
|
|
55
|
-
|
|
56
|
-
```javascript
|
|
57
|
-
import React from 'react';
|
|
58
|
-
import { withDi } from '@tramvai/react';
|
|
59
|
-
|
|
60
|
-
@withDi({ logger: LOGGER_TOKEN })
|
|
61
|
-
class BoxyPage extends Component {
|
|
62
|
-
render() {
|
|
63
|
-
this.props.logger.info('text');
|
|
64
|
-
return <div>Component</div>;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### useDiContainer
|
|
70
|
-
|
|
71
|
-
```tsx
|
|
72
|
-
type useDiContainer = () => DI.Container;
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
Getting an instance of a DI container that has been added to the application context.
|
|
76
|
-
|
|
77
|
-
It is better not to use this hook, as it is very low-level and is intended for developing new hooks
|
|
78
|
-
|
|
79
|
-
## Error
|
|
80
|
-
|
|
81
|
-
To handle errors during rendering, React uses [Error Boundary](https://ru.reactjs.org/docs/error-boundaries.html#introducing-error-boundaries). This package provides its own version of Error Boundary which will log an error through a generic logger and display a stub for the wrapped component if an error occurs.
|
|
82
|
-
|
|
83
|
-
### ErrorBoundary
|
|
84
|
-
|
|
85
|
-
Error Boundary component that monitors errors down the tree and, in case of a render error, will log an error and display the `fallbackComponent` component (passed as a props, by default it is a FallbackError from this package) instead of the fallen render subtree.
|
|
86
|
-
|
|
87
|
-
You can override the `fallbackComponent` through the `ERROR_BOUNDARY_FALLBACK_COMPONENT_TOKEN` provider.
|
|
88
|
-
|
|
89
|
-
### FallbackError
|
|
90
|
-
|
|
91
|
-
Component used by default as a stub for a subtree in which a render error occurred
|
|
92
|
-
|
|
93
|
-
### withError
|
|
94
|
-
|
|
95
|
-
Hook wrapping component in ErrorBoundary.
|
|
96
|
-
|
|
97
|
-
## lazy
|
|
98
|
-
|
|
99
|
-
To dynamically import components with SSR support, there is a high order `lazy` component:
|
|
100
|
-
|
|
101
|
-
```tsx
|
|
102
|
-
import { lazy } from '@tramvai/react';
|
|
103
|
-
|
|
104
|
-
const LazyComponent = lazy(() => import('./components/foo'), {
|
|
105
|
-
loading: <div>Загрузка...</div>,
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
<LazyComponent />;
|
|
109
|
-
```
|