-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Update react-redux docs for forwardRef #1119
Comments
Still a bit vague especially if I am approaching react-redux for the first time and trying to upgrade the codebase of an old project, for instance. Where do I need to use it and how can I pass it? Does this work?
Or is it something like this?
Or even this?
|
@luigimannoni : please look at the API docs more carefully: https://react-redux.js.org/api/connect The signature is: function connect(mapStateToProps?, mapDispatchToProps?, mergeProps?, options?) |
Well, I have tried and it does not work well at all: import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
NavigationContainer as NavigationContainerDefault,
} from '@react-navigation/native';
function NavigationContainer({ theme, ...rest }) {
return (
<NavigationContainerDefault theme={theme} {...rest} />
);
}
NavigationContainer.propTypes = {
/** @ignore */
theme: PropTypes.object.isRequired,
/** Settings allow you to use other icons than MaterialCommunityIcons */
settings: PropTypes.object,
};
NavigationContainer.defaultProps = {
settings: undefined,
};
const mapStateToProps = (state) => ({
theme: state.layout.themes[state.preferences.colorScheme],
});
const connectAndForwardRef = (
mapStateToProps = null,
mapDispatchToProps = null,
mergeProps = null,
options = {},
) => (component) => connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
{
...options,
forwardRef: true,
},
)(forwardRef(component));
const ConnectedNavigationContainer = connectAndForwardRef(mapStateToProps)(NavigationContainer);
export default ConnectedNavigationContainer; This result in : import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
NavigationContainer as NavigationContainerDefault,
} from '@react-navigation/native';
function NavigationContainer({ theme, ...rest }) {
return (
<NavigationContainerDefault theme={theme} {...rest} />
);
}
NavigationContainer.propTypes = {
/** @ignore */
theme: PropTypes.object.isRequired,
/** Settings allow you to use other icons than MaterialCommunityIcons */
settings: PropTypes.object,
};
NavigationContainer.defaultProps = {
settings: undefined,
};
const mapStateToProps = (state) => ({
theme: state.layout.themes[state.preferences.colorScheme],
});
const connectAndForwardRef = (
mapStateToProps = null,
mapDispatchToProps = null,
mergeProps = null,
options = {},
) => (component) => connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
{
...options,
forwardRef: true,
},
)(forwardRef(component));
const ConnectedNavigationContainer = connectAndForwardRef(mapStateToProps)(NavigationContainer);
export default ConnectedNavigationContainer;
|
@kopax you can't give a ref to a function component, you need to export it as a class |
I solved it with a function component..you need to stack useRef in connect and with the function. You should avoid class @dereknelson |
Hi @kopax ! Do you have any exemple please ? |
By stack I mean wrap your component with forwardRef as stated in reactjs documentation and also use the connect option with forwardRef true. Try with a simple component. Not all components from 3rd party libraries handle it correctly. Try with one of your own as I said and it will work |
A working solution with a functional component and connect :
|
Exactly, bravo:) |
And how would this work with class components? |
Just in case anyone is wondering with the @adrienlamotte example, you should still be able to pass along
|
Re this issue: #1118
After rereading the react redux docs, the reason I thought
withRef
could only be used with connectAdvanced is because withRef only appears under[connectOptions]
for connectAdvanced, and not regular connect. Had I read more carefully about connect I would've seen that it's a wrapper around connectAdvanced but I don't think it's obvious thatwithRef/forwardRef
can be passed to just connect as it's not under[options]
The text was updated successfully, but these errors were encountered: