@cleanweb/react 1.1.1-beta.5 → 1.1.1-beta.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,7 +70,9 @@ var MergedState = /** @class */ (function () {
70
70
  return MergedState;
71
71
  }());
72
72
  var useMergedState = function (initialState) {
73
- var cleanState = (0, react_1.useMemo)(function () { return new MergedState(initialState); }, []);
73
+ var cleanState = (0, react_1.useMemo)(function () {
74
+ return new MergedState(initialState);
75
+ }, []);
74
76
  MergedState.useRefresh.call(cleanState);
75
77
  return cleanState;
76
78
  };
@@ -1,8 +1,8 @@
1
1
  import type { TCleanState } from './state';
2
- export declare class ComponentMethods<TState extends object, TProps extends object> {
3
- state: TCleanState<TState>;
2
+ export declare class ComponentMethods<TProps extends object, TState extends object> {
4
3
  props: TProps;
4
+ state: TCleanState<TState>;
5
5
  }
6
- type UseMethods = <Class extends typeof ComponentMethods<object, object>>(Methods: Class & Constructor<InstanceType<Class>>, state: InstanceType<Class>['state'], props: InstanceType<Class>['props']) => InstanceType<Class>;
6
+ type UseMethods = <Class extends typeof ComponentMethods<object, object>>(Methods: Class & Constructor<InstanceType<Class>>, props: InstanceType<Class>['props'], state: InstanceType<Class>['state']) => InstanceType<Class>;
7
7
  export declare const useMethods: UseMethods;
8
8
  export {};
@@ -60,7 +60,7 @@ var ComponentMethods = /** @class */ (function () {
60
60
  }());
61
61
  exports.ComponentMethods = ComponentMethods;
62
62
  ;
63
- var useMethods = function (Methods, state, props) {
63
+ var useMethods = function (Methods, props, state) {
64
64
  // @todo Switch to useRef. Vite HMR seems to sometimes reinitialize useMemo calls after a hot update,
65
65
  // causing the instance to be unexpectedly recreated in the middle of the components lifecycle.
66
66
  // But useRef and useState values appear to always be preserved whenever this happens.
@@ -69,8 +69,8 @@ var useMethods = function (Methods, state, props) {
69
69
  // See useLogic implementation for a discussion of this type assertion.
70
70
  return new Methods();
71
71
  }, [])).current;
72
- methods.state = state;
73
72
  methods.props = props;
73
+ methods.state = state;
74
74
  return methods;
75
75
  };
76
76
  exports.useMethods = useMethods;
@@ -92,7 +92,7 @@ testing: {
92
92
  return [4 /*yield*/, import('./state.js')];
93
93
  case 1:
94
94
  useCleanState = (_a.sent()).useCleanState;
95
- self = (0, exports.useMethods)(MyMethods, useCleanState({}), {});
95
+ self = (0, exports.useMethods)(MyMethods, {}, useCleanState({}));
96
96
  return [2 /*return*/];
97
97
  }
98
98
  });
@@ -6,8 +6,9 @@ type o = object;
6
6
  export interface IComponentClass<Instance extends ClassComponent<o, o, o> = ClassComponent, Params extends ComponentClassParams = ComponentClassParams> extends IComponentInstanceClass<Instance, Params> {
7
7
  }
8
8
  type Extractor = <TComponent extends typeof ClassComponent<o, o, o>>(this: NonNullable<typeof _Component>, _Component?: TComponent & IComponentClass<InstanceType<TComponent>>) => VoidFunctionComponent<InstanceType<TComponent>['props']>;
9
- export declare class ClassComponent<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> extends ComponentInstance<TState, TProps, THooks> {
9
+ export declare class ClassComponent<TProps extends o = EmptyObject, TState extends o = EmptyObject, THooks extends o = EmptyObject> extends ComponentInstance<TState, TProps, THooks> {
10
10
  Render: VoidFunctionComponent<{}>;
11
+ readonly forceUpdate: VoidFunction;
11
12
  static renderAs: 'component' | 'template';
12
13
  static readonly FC: Extractor;
13
14
  }
@@ -32,6 +32,15 @@ var setFunctionName = function (func, newName) {
32
32
  console.warn(error);
33
33
  }
34
34
  };
35
+ var useRerender = function () {
36
+ /*
37
+ * Skip the value, we don't need it.
38
+ * Grab just the setter function.
39
+ */
40
+ var _a = (0, react_1.useState)(Date.now()), _forceRerender = _a[1];
41
+ var rerender = function () { return _forceRerender(Date.now()); };
42
+ return rerender;
43
+ };
35
44
  ;
36
45
  var ClassComponent = /** @class */ (function (_super) {
37
46
  __extends(ClassComponent, _super);
@@ -45,7 +54,12 @@ var ClassComponent = /** @class */ (function (_super) {
45
54
  if (!Component.getInitialState || !isClassComponentType)
46
55
  throw new Error('Attempted to initialize ClassComponent with invalid Class type. Either pass a class that extends ClassComponent to FC (e.g `export FC(MyComponent);`), or ensure it is called as a method on a ClassComponent constructor type (e.g `export MyComponent.FC()`).');
47
56
  var Wrapper = function (props, context) {
48
- var Render = (0, instance_1.useInstance)(Component, props).Render;
57
+ var instance = (0, instance_1.useInstance)(Component, props);
58
+ var Render = instance.Render;
59
+ var _forceUpdate;
60
+ // @ts-expect-error (Cannot assign to 'forceUpdate' because it is a read-only property.ts(2540))
61
+ instance.forceUpdate = (_forceUpdate = useRerender() // Moved this to separate line to allow TS errors. Use proxy local variable to regain some type checking for the assignment.
62
+ );
49
63
  // Add calling component name to Render function name in stack traces.
50
64
  (0, react_1.useMemo)(function () { return setFunctionName(Render, "".concat(Component.name, ".Render")); }, [Render]);
51
65
  /**
@@ -3,7 +3,7 @@ type AsyncAllowedEffectCallback = () => Awaitable<IVoidFunction>;
3
3
  type UseMountCallbacks = <TInstance extends ComponentInstance<any, any, any>>(instance: TInstance) => void;
4
4
  export declare const useMountCallbacks: UseMountCallbacks;
5
5
  export declare const noOp: () => void;
6
- export declare class ComponentInstance<TState extends object = EmptyObject, TProps extends object = EmptyObject, THooks extends object = EmptyObject> extends ComponentLogic<TState, TProps, THooks> {
6
+ export declare class ComponentInstance<TProps extends o = EmptyObject, TState extends o = EmptyObject, THooks extends o = EmptyObject> extends ComponentLogic<TState, TProps, THooks> {
7
7
  /**
8
8
  * Runs only _before_ first render, i.e before the component instance is mounted.
9
9
  * Useful for logic that is involved in determining what to render.
@@ -1,7 +1,7 @@
1
1
  import type { TCleanState, TState } from '../base/state';
2
2
  export type Empty = EmptyObject;
3
3
  type o = object;
4
- export declare class ComponentLogic<TState extends o = Empty, TProps extends o = Empty, THooks extends o = Empty> {
4
+ export declare class ComponentLogic<TProps extends o = Empty, TState extends o = Empty, THooks extends o = Empty> {
5
5
  state: TCleanState<TState>;
6
6
  props: TProps;
7
7
  hooks: THooks;
@@ -18,7 +18,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.useLogic = exports.ComponentLogic = void 0;
19
19
  var react_1 = require("react");
20
20
  var state_1 = require("../base/state");
21
- // React.Component
22
21
  var ComponentLogic = /** @class */ (function () {
23
22
  function ComponentLogic() {
24
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/react",
3
- "version": "1.1.1-beta.5",
3
+ "version": "1.1.1-beta.6",
4
4
  "description": "A suite of helpers for writing cleaner React function components.",
5
5
  "engines": {
6
6
  "node": ">=18"