question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-redux] injecting props HOC and forced exact type of OwnProps

See original GitHub issue

Currently OwnProps have to be Exact Type. Is it possible to use current connect<…> typings to create injecting-props HOC, like injectProp example in HOC docs?

https://flow.org/en/docs/react/hoc/

function injectProp<Config: {}>(
  Component: React.AbstractComponent<Config>
): React.AbstractComponent<$Diff<Config, {foo: number | void}>> {
  return function WrapperComponent(
    props: $Diff<Config, {foo: number | void}>,
  ) {
    return <Component {...props} foo={42} />;
  };
}

this doesn’t work and complains about OwnProps is incompatible with exact type

type Props = { ...OwnProps, foo: number };
type OwnProps = {};
const mapStateToProps = () => ({ foo: 5 });
export default connect<Props, OwnProps, _, _, _, _>(mapStateToProps);

(cc @peter-leonov)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
peter-leonovcommented, Jan 16, 2019

The example is unfortunately not working in my case

Maybe it’s because of my bad copy-paste skill 😃 Please, try using the code from the PR directly. It passes unit tests for types, even in Flow 0.90 😎

I don’t know if that is in the same scope, but i wanted to share one concern - I don’t know how recompose typings (which are old) do that, but for some reasons i don’t have to write annotations when using withProps recompose function. On the other hand, to use withProps i have to use HOC<...> type exported by recompose

  • it doesn’t work with React.AbstractComponent 😦

As far as I understood, the pre React.AbstractComponent type used everywhere React.ComponentType is now an alias type ComponentType<T> = AbstractComponent<T, any>. So, theoretically, it should be fine if other libraries internally used the proper types. But I’m not 100% sure.

Having the ability to just write one type annotation for the whole compose function makes it easier to work with. Imagine having 10 HOCs inside compose() function and having to annotate all the in/out props for every HOC

This is 100% true. I guess, Flow should allow this because (as far as I understand) it just needs the very tip of the exported type to be declared. If you get any success making it work, please, share 🤓

1reaction
peter-leonovcommented, Jan 13, 2019

Quick guess:

-type OwnProps = {};
+type OwnProps = {||};
Read more comments on GitHub >

github_iconTop Results From Across the Web

React-routing v6 can't pass component props to ...
The connect HOC however is injecting the collection prop, but ownProps is simply an empty object. const mapStateToProps = (state, ownProps) ...
Read more >
How to type react-redux style HOC in Typescript - Medium
So lets first type the mapProps function. This should be easy enough: We do not know the exact type of neither OwnProps nor...
Read more >
Usage with TypeScript - React Redux
The package includes a helper type, ConnectedProps , that can extract the return types of mapStateToProps and mapDispatchToProps from the first ...
Read more >
How to pass in an instance variable from a React component ...
I've now come up with an opinionated solution where the HoC injects two callback ... import PropTypes from 'prop-types' import movementIsStationary from '....
Read more >
Building a GraphQL-powered mobile application With React
By the end of this tutorial, you'll be able to write a ... The graphql HOC injects a data prop into the decorated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found