Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent useCallback implementation in ReactDOMServer #18783

Merged
merged 1 commit into from Apr 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -564,7 +564,7 @@ describe('ReactDOMServerHooks', () => {
});

describe('useCallback', () => {
itRenders('should ignore callbacks on the server', async render => {
itRenders('should not invoke the passed callbacks', async render => {
function Counter(props) {
useCallback(() => {
yieldValue('should not be invoked');
Expand All @@ -589,6 +589,34 @@ describe('ReactDOMServerHooks', () => {
expect(domNode.tagName).toEqual('SPAN');
expect(domNode.textContent).toEqual('Count: 5');
});

itRenders(
'should only change the returned reference when the inputs change',
async render => {
function CapitalizedText(props) {
const [text, setText] = useState(props.text);
const [count, setCount] = useState(0);
const capitalizeText = useCallback(() => text.toUpperCase(), [text]);
yieldValue(capitalizeText);
if (count < 3) {
setCount(count + 1);
}
if (text === 'hello' && count === 2) {
setText('hello, world.');
}
return <Text text={capitalizeText()} />;
}

const domNode = await render(<CapitalizedText text="hello" />);
const [first, second, third, fourth, result] = clearYields();
expect(first).toBe(second);
expect(second).toBe(third);
expect(third).not.toBe(fourth);
expect(result).toEqual('HELLO, WORLD.');
expect(domNode.tagName).toEqual('SPAN');
expect(domNode.textContent).toEqual('HELLO, WORLD.');
},
);
});

describe('useImperativeHandle', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Expand Up @@ -456,8 +456,7 @@ export function useCallback<T>(
callback: T,
deps: Array<mixed> | void | null,
): T {
// Callbacks are passed as they are in the server environment.
return callback;
return useMemo(() => callback, deps);
}

function useResponder(responder, props): ReactEventResponderListener<any, any> {
Expand Down