-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conflict with passing a "context" prop #1187
Comments
It is mentioned in the docs a couple times, but not particularly highlighted: https://react-redux.js.org/api/connect#context-object https://react-redux.js.org/using-react-redux/accessing-store#providing-custom-context Up through 5.x, In 6.0, Given that we're considering having our next version be a major version change anyway, perhaps we could consider renaming that prop to be something like |
My mistake, I had overlooked that in the docs. I'm perhaps at fault for using your comment on #1104 as a reference guide for the migration without completely reading the updated docs. I think you guys should be fine leaving it as Thank you for your response! I'll go ahead and close this issue. |
not sure if it's to late to alter the breaking changes 🤷♂️ but would have been great to have this noted somewhere :D debugged this for more than an hour till i found the problem. |
This comment has been minimized.
This comment has been minimized.
@markerikson Did y'all decided against renaming the prop to something like Just curious. Thanks! |
@markerikson @jmakGH What is the resolution to this? Can we change the name of the redux context prop? We use context all over our app, and it's not practical to rename it everywhere. |
Nope, there are not going to be any changes here. For one thing, we recommend using the hooks API instead of |
@markerikson With connect I was able to connect the same component with two different connect functions based on need. export fullComp = connect() vs export baseComp = connectBase(). Can something like this be replicated with the hooks? |
@strongui you will probably have to give more of an example what that was actually doing. |
@phryneas I basically have a component that can be a top level Component, that gets all data/methods from redux. But it can also be used as a "sub" component, where it gets methods from redux, but data from the parent. So I was able to export the same component but with two separate connect methods. I would just like to know if this can be replicated. Otherwise I have rename a prop across thousands of files because of this :/ |
I'm sorry, but this is a public API and we can't / won't change it at this point. It's also been the same for several years now. Is there a specific reason why this is suddenly a problem? Like Lenz said, if you can provide some actual example code demonstrating what you're doing, we may be able to offer other suggestions. |
@markerikson we have a huge app, we haven't had time to update until recently.
Basically this. Same component, but one gets data form redux, the other will get data from parent. |
@strongui something like this? function NonDataComponent({ data }) {
const dispatch = useDispatch()
// just use dispatch here to dispatch your actions
}
function FullComponent (){
const dataFromStore = useSelector(selectDataFromStore)
return <NonDataComponent data={dataFromStore} />
} |
@phryneas Nevermind I misread! I think that will work |
Out of curiosity, what version of React-Redux were you on prior to upgrading? Per the top of the |
We are on: The biggest problem is that it means I can't upgrade one piece at a time, since this error is all over the place where we use connect(), so I'll have to update everything to use hooks to avoid this. |
Hmm. I'm starting to vaguely remember some bits and pieces of details here. If I remember right, we did have some previous similar complaints, and I ended up implementing some complex internal checks to see AH. Found it - it was actually about a prop named so related but different. Can you post an actual repo or CodeSandbox and show the actual error you're getting? I'm still not quite following where the type clash is happening. |
@markerikson https://codesandbox.io/s/tender-mestorf-x272kj?file=/src/index.tsx are you able to see this? |
@strongui |
@phryneas @markerikson sorry for the typo, anyway now you see the TS error:
@phryneas although there doesn't seem any use for connect at all, this is just an example to show the error that I have. In our app "context" is used all over the place to signify "LIST" , "CARD", "SUMMARY" etc. So that is the TS error I am dealing with currency trying to update. I am not opposed to updating all to use toolkit if that is what needs to be done. I just wish I could do it in steps, because this will be a massive undertaking for a prop name :/ |
There is indeed a failsafe check in place to only use that react-redux/src/components/connect.tsx Lines 530 to 539 in fb8cfc0
So this could at that point be accommodated with a TS only change. |
Hmm. Interesting. If I fix the use of I wonder what about our types changed there? It most likely had something to do with the TS conversion process. The internal logic for "what do we do with const ContextToUse: ReactReduxContextInstance = useMemo(() => {
// Users may optionally pass in a custom context instance to use instead of our ReactReduxContext.
// Memoize the check that determines which context instance we should use.
return propsContext &&
propsContext.Consumer &&
// @ts-ignore
isContextConsumer(<propsContext.Consumer />)
? propsContext
: Context
}, [propsContext, Context])
// Retrieve the store and ancestor subscription via context, if available
const contextValue = useContext(ContextToUse) It's probably this part: export interface ConnectProps {
/** A custom Context instance that the component can use to access the store from an alternate Provider using that same Context instance */
context?: ReactReduxContextInstance
/** A Redux store instance to be used for subscriptions instead of the store from a Provider */
store?: Store
}
interface InternalConnectProps extends ConnectProps {
reactReduxForwardedRef?: React.ForwardedRef<unknown>
}
function ConnectFunction<TOwnProps>(
props: InternalConnectProps & TOwnProps
) {
// omit
} Given that, I think this would run, but it won't compile, which is an issue. I don't immediately have a suggestion to work around this (also I'm context-switching (HAH!) from work to this thread every couple minutes, which isn't helping) |
So, if I'm reading this correctly, our |
@strongui to be clear, this doesn't actually have anything to do with Redux Toolkit at all. This is strictly the TS types for |
actually. I bet we can do some fancy footwork with Like, "if |
@markerikson oh are you saying the typings are not correct, since it technically is not reflecting what is actually happening in the code? It would be pretty awesome if it was just that as a fix, it would make upgrading a breeze! Who maintains those? How can I help? |
I've investigated some more, and it doesn't seem like a fix to this is possible just in the typings
For now I've just created a patch version in our app to deal with this. |
@markerikson I have updated my sandbox to use the version from the PR https://codesandbox.io/s/tender-mestorf-x272kj Unfortunately it does not seem to fix it. |
@markerikson different issue, different fix ;) |
Oh, whoops, misread that, sorry! |
Upon upgrading React and React Redux to the latest versions, an app I am working on started crashing on render.
After some debugging I traced the issue to a connected component being passed a "context" prop that was being used to control some of the app's features and completely unrelated to React's Context API.
I couldn't find any notes about that being a protected prop anywhere in the docs or online and thought it might be a good idea to post this here in case anyone else happened to stumble across this issue.
The biggest takeaway was that the only error message came from React, which made debugging the issue kind of a hassle -- especially because everything was working prior to upgrading to the latest version (which makes sense since the version of React we were using was before their Context update).
Perhaps a note under the Troubleshooting section of the website could document this issue as a potential pitfall.
Issue is recreated at this CodeSandbox. The app crashes whenever a context prop is passed to ConnectedChild.
Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?
React 16.8.1
ReactDOM 16.8.1
React Redux 6.0.0
This issue wasn't happening with React v15 and React Redux v4
The text was updated successfully, but these errors were encountered: