With Composition in React we mean the method to split up data management and pure rendering components, composition is actually a known method in mathematics, “the pointwise application of one function to the result of another to produce a third function” (ref. Function Composition).
Composition can be achieved with known methods (createContainer, TrackerReact and react-komposer) by passing your data management function to the compostion method which resolves in a method that takes your Component as an argument.
With Tracker.Component this can be achivieved with:
I don’t think a lot of people knows about it yet, it was released earlier this week so that might be a reason. I haven’t done any performance tests yet, that would be great for the comparison actually, I’m curious if you have any ideas/tips on what to start with doing tests, I’m still a bit new to tests in Meteor 1.3.
It is not easy to understand for me, or maybe I missed some recent development. From the code, I can see that Cars is a function that returns a React spec tree. From the usage (<Cars />) one I must make the (possibly false) assumption that the Composition component will pass the info that the Cars function needs. I also don’t see where MainLayout is ever used (remove it perhaps?).
If the code was more explicit (less magic) it’d be easier to understand.
@timbrandin Would you mind explaining what’s happening in the example?
The implementation to being a Composing method is really easy, and I’ve implemented it just like how any other composition works, just have a look at ReactMeteorData’s createComponent:
This is basically the same as what we’re doing in Tracker.Component:
The one difference is that if you pass in multiple children into the composition all of them get the state as props:
<Composition brand={ 'Volvo' }>
<Cars>Yeah! I have: `props.brand="Volvo"`</Cars>
</Composition>
This idea is heavily inspired by React Router so I guess it is a good choice?