Skip to content

Commit

Permalink
Coerce permalink to string
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Aug 29, 2023
1 parent c538193 commit 3216b92
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,45 @@ describe('ReactFlightDOMForm', () => {

expect(form.target).toBe('/permalink');
});

// @gate enableFormActions
// @gate enableAsyncActions
it('useFormState `permalink` is coerced to string', async () => {
const serverAction = serverExports(function action(prevState) {
return {state: prevState.count + 1};
});

class Permalink {
toString() {
return '/permalink';
}
}

const permalink = new Permalink();

const initialState = {count: 1};
function Client({action}) {
const [state, dispatch] = useFormState(action, initialState, permalink);
return (
<form action={dispatch}>
<span>Count: {state.count}</span>
</form>
);
}
const ClientRef = await clientExports(Client);

const rscStream = ReactServerDOMServer.renderToReadableStream(
<ClientRef action={serverAction} />,
webpackMap,
);
const response = ReactServerDOMClient.createFromReadableStream(rscStream);
const ssrStream = await ReactDOMServer.renderToReadableStream(response);
await readIntoContainer(ssrStream);

const form = container.firstChild;
const span = container.getElementsByTagName('span')[0];
expect(span.textContent).toBe('Count: 1');

expect(form.target).toBe('/permalink');
});
});
8 changes: 6 additions & 2 deletions packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
REACT_CONTEXT_TYPE,
REACT_MEMO_CACHE_SENTINEL,
} from 'shared/ReactSymbols';
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';

type BasicStateAction<S> = (S => S) | S;
type Dispatch<A> = A => void;
Expand Down Expand Up @@ -575,8 +576,11 @@ function useFormState<S, P>(
// $FlowIgnore[prop-missing]
const metadata: ReactCustomFormAction = boundAction.$$FORM_ACTION(prefix);
// Override the target URL
if (typeof permalink === 'string') {
metadata.target = permalink;
if (permalink !== undefined) {
if (__DEV__) {
checkAttributeStringCoercion(permalink, 'target');
}
metadata.target = permalink + '';
}
return metadata;
};
Expand Down

0 comments on commit 3216b92

Please sign in to comment.