forked from Firnis/yandex-metrica-ab-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
56 lines (45 loc) · 1.85 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React, { useMemo } from 'react';
import ReactDOM from 'react-dom';
import { useExperiments } from '../useExperiments';
import { ProviderApp, ProviderNoFlickerApp } from './provider';
import { clientId } from './clientId';
import { Flags } from './flags';
import { UseFlagComponent } from './useFlag';
// Кнопка будет перерисована после получения флагов. То есть мигнёт
const Button: React.FC = (props) => {
const { flags } = useExperiments<typeof Flags>({
clientId,
});
const flagVal = useMemo(() => flags.flag_exp?.[0], [flags]);
return <button style={{ backgroundColor: flagVal || '#ccc' }} { ...props }>{ String(flagVal || 'default').toUpperCase() }</button>;
}
// Кнопка будет нарисована только после получения флагов.
const ButtonRenderAfterFlags: React.FC = (props) => {
const { flags, ready } = useExperiments<typeof Flags>({
clientId,
});
const flagVal = useMemo(() => flags.flag_exp?.[0], [flags]);
if (!ready) {
return null;
}
return <button style={{ backgroundColor: flagVal || '#ccc' }} { ...props }>{ String(flagVal || 'default').toUpperCase() }</button>;
}
const ButtonClassName: React.FC = (props) => {
const { flags } = useExperiments<typeof Flags>({
clientId,
});
const flagVal = useMemo(() => flags.flag_exp?.[0], [flags]);
const className = useMemo(() => flagVal ? 'button__experiment' : 'button', [flagVal]);
return <button className={ className } { ...props }>{ String(flagVal || 'default').toUpperCase() }</button>;
}
ReactDOM.render(
<div>
<Button />
<ButtonRenderAfterFlags />
<ButtonClassName />
<ProviderApp />
<ProviderNoFlickerApp />
<UseFlagComponent />
</div>,
document.getElementById('root'),
)