Skip to content

Commit 929920b

Browse files
committed
Don't override ref property when not configured to.
1 parent a9129a5 commit 929920b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/components/connect.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,14 @@ function connect<
747747
// Now that all that's done, we can finally try to actually render the child component.
748748
// We memoize the elements for the rendered child component as an optimization.
749749
const renderedWrappedComponent = React.useMemo(() => {
750+
const effectiveProps = {...actualChildProps};
751+
if (forwardRef) {
752+
effectiveProps.ref = reactReduxForwardedRef
753+
}
750754
return (
751755
// @ts-ignore
752756
<WrappedComponent
753-
{...actualChildProps}
754-
ref={reactReduxForwardedRef}
757+
{...effectiveProps}
755758
/>
756759
)
757760
}, [reactReduxForwardedRef, WrappedComponent, actualChildProps])

test/components/connect.spec.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,6 +2588,25 @@ describe('React', () => {
25882588

25892589
expect(tester.getByTestId('a')).toHaveTextContent('42')
25902590
})
2591+
2592+
it('should not override ref property when not asked to.', async () => {
2593+
const store = createStore(() => ({}))
2594+
interface RefReceiverProps {
2595+
ref: React.Ref<HTMLSpanElement | null>
2596+
}
2597+
function RefReceiver(props: RefReceiverProps) {
2598+
const {ref} = props
2599+
return <span ref={ref} />
2600+
}
2601+
const DecoratedRefReceiver = connect()(RefReceiver)
2602+
const testRef = React.createRef<HTMLSpanElement>()
2603+
rtl.render(
2604+
<ProviderMock store={store}>
2605+
<DecoratedRefReceiver ref={testRef} />
2606+
</ProviderMock>
2607+
)
2608+
expect(testRef.current).toBeInstanceOf(HTMLSpanElement);
2609+
})
25912610
})
25922611

25932612
describe('Factory functions for mapState/mapDispatch', () => {

0 commit comments

Comments
 (0)