Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fep-2635-observe-prevented-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackflow/core": minor
---

Expose the live, action-local `actions.isPrevented()` state to every pre-effect hook without stopping the remaining hooks.
5 changes: 5 additions & 0 deletions .changeset/fep-2635-skip-prevented-loader-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackflow/react": patch
---

Require `@stackflow/core` v3 and skip loader, preload, and pause work for prevented push and replace actions.
1 change: 1 addition & 0 deletions core/src/interfaces/StackflowPluginHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type StackflowPluginInitHook = (args: {
export type StackflowPluginPreEffectHook<T> = (args: {
actionParams: T;
actions: StackflowActions & {
isPrevented: () => boolean;
preventDefault: () => void;
overrideActionParams: (params: T) => void;
};
Expand Down
1 change: 1 addition & 0 deletions core/src/utils/triggerPreEffectHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function triggerPreEffectHook<K extends ActionName>(
actionParams: { ...nextActionParams },
actions: {
...actions,
isPrevented: () => isPrevented,
preventDefault: () => {
isPrevented = true;
},
Expand Down
3 changes: 3 additions & 0 deletions docs/pages/docs/advanced/write-plugin.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,14 @@ Pre-effect hooks include `onBeforePush`, `onBeforeReplace`, and `onBeforePop`. P
| | | |
| ---------------------- | ---------- | ------------------------------------------ |
| actions.preventDefault | `function` | Cancel the default behavior. |
| actions.isPrevented | `function` | Check whether the action is prevented. |
| actions.getStack | `function` | Get the current stack state. |
| actions.dispatchEvent | `function` | Add a new event to the core. |
| effect | `object` | The effect that triggered the effect hook. |
</APITable>

Calling `preventDefault()` cancels the default event dispatch, but it does not stop the remaining pre-effect hooks. Those hooks can call `isPrevented()` to observe the current action-local state.

## Determining initial activity

You can override the existing `initialActivity` behavior through the `overrideInitialEvents` API.
Expand Down
3 changes: 3 additions & 0 deletions docs/pages/docs/advanced/write-plugin.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,14 @@ Pre-effect 훅에는 `onBeforePush`, `onBeforeReplace`, `onBeforePop`이 있어
| | | |
| ---------------------- | ---------- | ----------------------------- |
| actions.preventDefault | `function` | (추가) 기본 동작을 취소해요. |
| actions.isPrevented | `function` | 현재 action의 취소 상태를 확인해요. |
| actions.getStack | `function` | 현재 스택의 상태를 가져올 수 있어요. |
| actions.dispatchEvent | `function` | 코어에 새 이벤트를 추가해요. |
| effect | `object` | 해당 이펙트 훅을 촉발시킨 이펙트에요. |
</APITable>

`preventDefault()`를 호출하면 기본 이벤트 dispatch는 취소되지만 남은 pre-effect 훅은 계속 실행돼요. 후속 훅은 `isPrevented()`를 호출해 현재 action에 한정된 취소 상태를 확인할 수 있어요.

## 첫 액티비티 결정하기

`overrideInitialEvents` API를 통해 기존에 존재하는 `initialActivity` 동작을 덮어쓸 수 있어요.
Expand Down
2 changes: 1 addition & 1 deletion integrations/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"peerDependencies": {
"@stackflow/config": "^2.0.0",
"@stackflow/core": "^2.0.0 || ^3.0.0",
"@stackflow/core": "^3.0.0",
"@types/react": ">=16.8.0",
"react": ">=16.8.0"
},
Expand Down
10 changes: 6 additions & 4 deletions integrations/react/src/loader/loaderPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ function createBeforeRouteHandler<
input: StackflowInput<T, R>,
loadData: (activityName: string, activityParams: {}) => unknown,
): OnBeforeRoute {
return ({
actionParams,
actions: { overrideActionParams, pause, resume },
}) => {
return ({ actionParams, actions }) => {
if (actions.isPrevented()) {
return;
}

const { overrideActionParams, pause, resume } = actions;
const { activityName, activityParams, activityContext } = actionParams;

const matchActivity = input.config.activities.find(
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6068,7 +6068,7 @@ __metadata:
typescript: "npm:^5.5.3"
peerDependencies:
"@stackflow/config": ^2.0.0
"@stackflow/core": ^2.0.0 || ^3.0.0
"@stackflow/core": ^3.0.0
"@types/react": ">=16.8.0"
react: ">=16.8.0"
languageName: unknown
Expand Down
Loading