effector-factorio icon indicating copy to clipboard operation
effector-factorio copied to clipboard

Why `model` prop is passed to original view component?

Open 7iomka opened this issue 2 years ago • 4 comments

Hi. Why model prop is passed to original view component? This is needed only for types? Because I wasn't expected to receive it alongside with my component props.

const FeatureComponentSelect = modelView(factory, (props: SelectProps) => { const model = factory.useModel(); // some code inside return <Select {...props} /> })

So in the DOM I have model prop passed. So this is a problem, and I need somehow to make 2 steps to solve this issue

  1. every type of component wrapper I need to mix to something like this props: SelectProps & { model: ReturnType<typeof factory.useModel> }
  2. if component passed rest props with spread, every time you need to extract model prop from it

So, my question is - why you pass model prop to original component if you never use it (model is always is extracted through factory.useModel hook)

My suggestion is to separate viewProps from model like this

 const { model, ...viewProps } = props;

7iomka avatar Feb 24 '23 08:02 7iomka

It is useful in cases where you don't have children components but you want to create reusable feature. Created view has "model" field in it's props signature. Reading types is better than expecting.

mistical2008 avatar Nov 02 '23 13:11 mistical2008

99% of components in our projects use model directly from props And just like 2-3 times I was needed to use useModel hook to get model in child components

SQReder avatar Dec 27 '23 13:12 SQReder

99% of components in our projects use model directly from props And just like 2-3 times I was needed to use useModel hook to get model in child components

This means that you need to add type of model in component props in 99% cases?

7iomka avatar Dec 27 '23 14:12 7iomka

99% of components in our projects use model directly from props And just like 2-3 times I was needed to use useModel hook to get model in child components

This means that you need to add type of model in component props in 99% cases?

I think it's kinda cheap

type Props = {
    model: Model<typeof myCoolModelFactory>
    otherProp: unknown
}

const MyCoolView = modelView(myCoolModelFactory, ({model, otherProp}: Props) =>{
    return null;
})

Is you haven't any extra props, so better

const MyCoolView = modelView(myCoolModelFactory, ({model}) =>{
    return null;
})

SQReder avatar Mar 05 '24 08:03 SQReder