Use props in HoC parameters



I’m trying to use props values in my HoC but i’m struggling quiet hard

Here is my code :

export default compose(
  connectEndpoint({
    discussion: getDiscussionRequest(
      props.message.discussionid,
      {
        refreshing: true
      },
      props.message.flag
    ),
    contacts: getContactsByFlagRequest({ refreshing: true }, props.message.flag)
  }),

and here is the connectEndpoint function :

export default requests =>
  compose(
    connect(() =>
      withSingleApi(typeof requests === "function" ? requests() : requests)
    )
  );

I’m trying to use my props value when i call “connectEndpoint” but i don’t know what syntax should i use to have access to “props”. I tried to use “withProps” but with no success on this case

Thanks in advance!

Answer

Since you are using connect within your connectEndPoint HOC , you can implement the requests property of your connectEndPoint HOC as a function and pass on the state and props from connect to it.

export default requests =>
  compose(
    connect((state, props) =>
      withSingleApi(typeof requests === "function" ? requests(state, props) : requests)
    )
  );

and use it like

export default compose(
  connectEndpoint((state, props) => ({
    discussion: getDiscussionRequest(
      props.message.discussionid,
      {
        refreshing: true
      },
      props.message.flag
    ),
    contacts: getContactsByFlagRequest({ refreshing: true }, props.message.flag)
  }));

The reason you were not receiving props in your implementation was because connectEndPoint is a function that is being called and only the return value from it is being used to render the component which receives the props



Source: stackoverflow