sizes property doesn't is not a dependency #64
Replies: 3 comments 10 replies
-
Hi @photoskiff. Thanks for the feedback. I put in the I think that's a sensible decision but there are some actions which it does make sense to expose programatically. Particularly, the one you mention, resetting the layout (the same as double clicking). I could add a method to the component's ref which you could call to reset the layout, e.g. I will also add some documentation making it clear what the behaviour of the |
Beta Was this translation helpful? Give feedback.
-
Hmmmm, the more I think about it, the more I can see a use case for setting the sizes of the pane s programmatically. It would have to be done by a method on the component reference because it's an imperative type of operation rather than a declarative prop. I'll mull this over. |
Beta Was this translation helpful? Give feedback.
-
Sure, let me try and explain where I'm coming from 🙂. React components are intended to be declarative. In the sense that the props you pass in will be reflected in the output (usually html). A common exception are default or initial props. Which is how sizes works in Allotment. A good example would be an uncontrolled component behaving like an input element. You would pass a defaultValue prop but after its mounted it manages its own state. For performance sensitive components (like Allotment) which might generate a lot of updates it's often beneficial to make them uncontrolled and let them manage state outside of the React lifecycle if necessary (which is what Allotment does). What I think you're describing is neither a declarative controlled component (i.e. pass in sizes and that's what you see, onChange could be used to close the loop) nor an uncontrolled component (i.e. pass in initial sizes but from then on the component is in charge). I usually advise developers not to implement components which treat their state as a side-effect. You often see this when a component performs a particular action (like resetting) when a prop changes value. This sort of 'change detection' is sometimes a poor man's attempt at implementing something which would be better off as an action (or event). In the case of Allotment we could add a setSizes() method. I hear what you're saying about having to manage the refs but you could accomplish what you're after by putting a wrapper around the Allotment component which detects when sizes has changed and calls setSize. I feel that type of behaviour is better off in the calling code rather than the library. Another way to think about this is what should happen in the following case:
But you can't simply pass in the same array as it wouldn't trigger the change. You could pass in a new array with the same elements but at that point you're really fighting against React. Btw I appreciate all your input and I'm not saying I'm necessarily correct in what I say. I'm basing it on my own experience. |
Beta Was this translation helpful? Give feedback.
-
Hi John,
It doesn't seem to be possible to update sizes programmatically (reset for instance) since the useEffect doesn't have a dependency on sizes property.
Very nice component btw, I like how it manages a group of panes
Beta Was this translation helpful? Give feedback.
All reactions