freactal icon indicating copy to clipboard operation
freactal copied to clipboard

Clarify integration with class components and lifecycle methods

Open aweary opened this issue 8 years ago • 11 comments

The idea of deferring state management to freactal is solid but as far as I can tell, users will still need to use class components if they want to trigger effects in lifecycle methods, and the README seems to imply that you can always use SFCs because freactal is orchestrating state, but that feels slightly misleading since React provides more than just state management APIs

Maybe add an example uses a stateless class component triggering effects in lifecycle methods to make it clear that stateless doesn't always mean functional components?

aweary avatar May 05 '17 02:05 aweary

HI @aweary , you can use recompose / recompact lifecycle() to do that.

tlvenn avatar May 05 '17 03:05 tlvenn

Is exactly as @tlvenn mentioned, I just wanted to add that you are able to use this, as effects are received on props, and you can use props there in the fns you provide to lifecycle, so this way you can trigger any effect you want on any lifecycle method (fn).

mgtitimoli avatar May 05 '17 03:05 mgtitimoli

I understand that it's possible, this issue is about clarifying that in the documentation.

aweary avatar May 05 '17 03:05 aweary

Also lifecycle() or other composition utilities are just wrappers around class components. Their abstractions may be more consistent with freactal, but we should still demonstrate usage with lifecycle methods without requiring another level of abstraction

aweary avatar May 05 '17 03:05 aweary

But from the point of view of the state, that is what freactal solves, it's OK to say that you could always use SFCs, what you are saying is more about the fact that you can't when you have lifecycle methods unless you use recompose/recompact, or as you are saying use a class component, what I mean with this, that it's not something related to freactal but more with react itself.

mgtitimoli avatar May 05 '17 03:05 mgtitimoli

but as far as I can tell, users will still need to use class components if they want to trigger effects in lifecycle methods

The wording your choose kinda led us to believe otherwise ;) But yes I agree, this should be documented and people shouldnt shy away from recompose/recompact with freactal.

In that regard, I dont think the doc should hint that freactal can potentially replace recompose. Imho, they complement each other. The only thing it will replace is the need to use the withState that recompose is offering.

tlvenn avatar May 05 '17 03:05 tlvenn

But from the point of view of the state, that is what freactal solves, it's OK to say that you could always use SFCs, what you are saying is more about the fact that you can't when you have lifecycle methods unless you use recompose/recompact

What I'm saying is that freactal provides no way to orchestrate effects based on React lifecycle methods. It stresses the point that you can use just SFCs, which you can in contexts where state updates are triggered from subscribers registered in render like event listeners.

But real world applications will often trigger state updates in lifecycle methods, and it's worth documenting how this works because the current emphasis on SFCs and the lack of class lifecycle method examples leaves it ambiguous.

aweary avatar May 05 '17 03:05 aweary

Agreed, @aweary. Let's get a solid example in there. Somewhere in the guide, maybe?

divmain avatar May 08 '17 07:05 divmain

Was looking for a how-to guide to do exactly this and found this thread


edit: nvm, wrapped a class component and it works just fine

cheapsteak avatar Oct 06 '17 01:10 cheapsteak

@cheapsteak did you mean composed instead of wrapped, for example

class Root extends React.Component {
  render() {
    return <h1>Root</h1>
  }
}

const Parent = wrapComponentWithState(injectState(({ state }) => (
  <Root state={ state }/>
)));

export default Parent

Something like that?

AddictArts avatar Feb 20 '18 20:02 AddictArts

Yup! Exactly what I was wondering about, I should have just tried it out

cheapsteak avatar Feb 20 '18 20:02 cheapsteak