Skip to content

Commit

Permalink
fix: parent store lifecycle
Browse files Browse the repository at this point in the history
- create relation context for components without relative stores
  • Loading branch information
MatthewPattell committed Apr 19, 2024
1 parent 29d4905 commit f20c5e1
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 116 deletions.
File renamed without changes.
12 changes: 7 additions & 5 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 4 additions & 21 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "example-react-mobx-manager",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "vite",
"build": "vite build"
},
"dependencies": {
"@lomray/consistent-suspense": "^2.0.1",
"@lomray/react-mobx-manager": "^3.0.1",
Expand All @@ -20,26 +24,5 @@
"sass": "^1.70.0",
"vite": "^5.0.12",
"typescript": "^4.7.4"
},
"scripts": {
"start": "vite",
"build": "vite build"
},
"eslintConfig": {
"extends": [
"react-app"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
10 changes: 0 additions & 10 deletions example/src/pages/user/components/info/extra-info/index.stores.ts

This file was deleted.

11 changes: 7 additions & 4 deletions example/src/pages/user/components/info/extra-info/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { withStores } from '@lomray/react-mobx-manager';
import { type StoresType, withStores } from "@lomray/react-mobx-manager";
import type { FC } from 'react';
import React, { useEffect } from 'react';
import type { StoreProps } from './index.stores';
import stores from './index.stores';
import ExtraInfoStore from './stores/main';

const stores = {
extraInfoStore: ExtraInfoStore,
};

/**
* ExtraInfo (children) component
* Demonstrate working with store in children component
* @constructor
*/
const ExtraInfo: FC<StoreProps> = ({ extraInfoStore: { phone, getExtraInfo } }) => {
const ExtraInfo: FC<StoresType<typeof stores>> = ({ extraInfoStore: { phone, getExtraInfo } }) => {
useEffect(() => {
void getExtraInfo();
}, [getExtraInfo]);
Expand Down
10 changes: 0 additions & 10 deletions example/src/pages/user/index.stores.ts

This file was deleted.

11 changes: 7 additions & 4 deletions example/src/pages/user/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { withStores } from '@lomray/react-mobx-manager';
import { type StoresType, withStores } from "@lomray/react-mobx-manager";
import type { FC } from 'react';
import React, { useEffect } from 'react';
import Info from './components/info';
import type { StoreProps } from './index.stores';
import stores from './index.stores';
import UserPageStore from './stores/main';

interface IUser {
userId: string;
}

type Props = IUser & StoreProps;
const stores = {
userPage: UserPageStore,
};

type Props = IUser & StoresType<typeof stores>;

const User: FC<Props> = ({ userId, userPage: { user, error, isLoading, getUser } }) => {
useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export const ROOT_CONTEXT_ID = 'root';
11 changes: 6 additions & 5 deletions src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FC, ReactElement } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { ROOT_CONTEXT_ID } from './constants';
import type Manager from './manager';
import type { TStores } from './types';

Expand All @@ -13,7 +14,7 @@ interface IStoreManagerProvider {
interface IStoreManagerParentProvider {
parentId: string;
children?: React.ReactNode;
initStores?: TStores;
touchableStores?: TStores;
}

/**
Expand All @@ -25,7 +26,7 @@ const StoreManagerContext = React.createContext<Manager>({} as Manager);
* To spread relationships
*/
const StoreManagerParentContext =
React.createContext<IStoreManagerParentProvider['parentId']>('root');
React.createContext<IStoreManagerParentProvider['parentId']>(ROOT_CONTEXT_ID);

/**
* Mobx store manager parent provider
Expand All @@ -34,12 +35,12 @@ const StoreManagerParentContext =
const StoreManagerParentProvider: FC<Omit<IStoreManagerParentProvider, 'contextId'>> = ({
parentId,
children,
initStores,
touchableStores,
}) => {
const storeManager = useStoreManager();

if (initStores) {
storeManager.touchedStores(initStores);
if (touchableStores) {
storeManager.touchedStores(touchableStores);
}

return <StoreManagerParentContext.Provider value={parentId} children={children} />;
Expand Down
Loading

0 comments on commit f20c5e1

Please sign in to comment.