Skip to content

Commit 16ccb6d

Browse files
committed
fix context
1 parent 8e923cd commit 16ccb6d

File tree

8 files changed

+213
-238
lines changed

8 files changed

+213
-238
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ Server component handles retrieving values from context
8080
```tsx
8181
"use server";
8282

83-
import { useMixContext } from "next-approuter-context";
8483
import { context1, context2 } from "./context";
84+
import { getMixContext } from "./next-approuter-context";
8585

86-
export const Server = () => {
87-
// If the component is async, it should be written as follows
88-
// const { text, color } = await getMixContext<ContextType1>();
89-
const { text, color } = useMixContext(context1);
90-
const value = useMixContext(context2);
86+
export const Server = async () => {
87+
const { text, color } = await getMixContext(context1);
88+
const value = await getMixContext(context2);
9189
return (
9290
<>
9391
<div style={{ color }}>

esm/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
{
22
"name": "next-approuter-context",
3-
"version": "0.0.9",
4-
"main": "./dist/cjs/index.js",
5-
"types": "./dist/cjs/index.d.ts",
6-
"exports": {
7-
"require": "./dist/cjs/index.js",
8-
"import": "./dist/esm/index.js"
9-
},
3+
"version": "0.2.2",
4+
"main": "./dist/index.js",
5+
"types": "./dist/index.d.ts",
106
"author": "SoraKumo <[email protected]>",
117
"license": "MIT",
128
"scripts": {
13-
"build": "rimraf dist && tsc && tsc -p ./tsconfig.esm.json && cpy esm dist",
9+
"build": "rimraf dist && tsc",
1410
"watch": "tsc -b -w",
1511
"lint": "eslint ./src",
1612
"lint:fix": "eslint --fix ./src"
1713
},
1814
"devDependencies": {
19-
"@types/node": "20.10.0",
20-
"@types/react": "^18.2.38",
21-
"@typescript-eslint/eslint-plugin": "^6.12.0",
22-
"@typescript-eslint/parser": "^6.12.0",
15+
"@types/node": "20.11.25",
16+
"@types/react": "^18.2.64",
17+
"@typescript-eslint/eslint-plugin": "^7.1.1",
18+
"@typescript-eslint/parser": "^7.1.1",
2319
"cpy-cli": "^5.0.0",
24-
"eslint": "8.54.0",
25-
"eslint-config-prettier": "^9.0.0",
20+
"eslint": "8.57.0",
21+
"eslint-config-prettier": "^9.1.0",
2622
"eslint-import-resolver-typescript": "^3.6.1",
27-
"eslint-plugin-import": "^2.29.0",
28-
"next": "^14.0.3",
23+
"eslint-plugin-import": "^2.29.1",
24+
"next": "^14.1.3",
2925
"rimraf": "^5.0.5",
30-
"typescript": "^5.3.2"
26+
"typescript": "^5.4.2"
3127
},
3228
"repository": "https://github.com/ReactLibraries/next-approuter-context",
3329
"keywords": [

src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
export { createMixServerContext } from "./server";
2-
export * from "./server.js";
3-
export * from "./client.js";
4-
51
import React, { use } from "react";
6-
import { useClientContext } from "./client";
7-
import { context, createMixServerContext } from "./server";
2+
import { useClientContext } from "./client.js";
3+
import { Exports, getMixContext } from "./server.js";
4+
5+
export { getMixContext } from "./server";
6+
export const createMixServerContext = Exports.createMixServerContext;
87

98
export const getComponentType = () =>
109
!React["useState"]
@@ -13,9 +12,11 @@ export const getComponentType = () =>
1312
? "Pages"
1413
: "Client";
1514

16-
export const useMixContext = <T>({ name }: { name: string; type: T }) => {
17-
if (getComponentType() === "Server") return use(context().get<T>(name));
18-
return useClientContext<T>(name);
15+
export const useMixContext = <T>(context: { name: string; type: T }) => {
16+
if (getComponentType() === "Server") {
17+
return use(getMixContext<T>(context));
18+
}
19+
return useClientContext<T>(context.name);
1920
};
2021

2122
export const createMixContext = <T>(name: string) => {

src/server.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ReactNode, cache } from "react";
33
import { ClientProvider } from "./client.js";
44

5-
export const context = cache(() => {
5+
const context = cache(() => {
66
const property: {
77
[key: string]: {
88
resolve: (value: unknown) => void;
@@ -29,7 +29,8 @@ export const context = cache(() => {
2929
};
3030
});
3131

32-
export const createMixServerContext = <T,>(name: string = "") => {
32+
export const Exports = async () => {};
33+
Exports.createMixServerContext = <T,>(name: string = "") => {
3334
const Provider = ({ children, value }: { children: ReactNode; value: T }) => {
3435
context().set(name, value);
3536
return (
@@ -46,4 +47,5 @@ export const createMixServerContext = <T,>(name: string = "") => {
4647
return result;
4748
};
4849

49-
export const getMixContext = <T,>(name: string = "") => context().get<T>(name);
50+
export const getMixContext = async <T,>({ name }: { name: string; type: T }) =>
51+
context().get<T>(name);

tsconfig.esm.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2626

2727
/* Modules */
28-
"module": "CommonJS" /* Specify what module code is generated. */,
28+
"module": "ESNext" /* Specify what module code is generated. */,
2929
"rootDir": "./src" /* Specify the root folder within your source files. */,
3030
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
@@ -55,7 +55,7 @@
5555
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
5656
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
5757
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
58-
"outDir": "./dist/cjs" /* Specify an output folder for all emitted files. */,
58+
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
5959
// "removeComments": true, /* Disable emitting comments. */
6060
// "noEmit": true, /* Disable emitting files from a compilation. */
6161
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */

0 commit comments

Comments
 (0)