javascript - Functional style of ReactJs breaks encapsulation -


i can't right. here thing: made 2 widgets, tabbar , tabnavigator. tabnav contains state index currentselectedtab. when tabbar receives user click, notify parent (tabnav) (with callback), , precisely parent inject new props saying: dear tabbar, select tab: currentselectedtab , @ same time, tabnav display right content tab.

so far good. but, want application able set tabnav specific tab. introduce that, props, breaks encapsulation of currentselectedtab in tabnav. must defined outside, in application. , sucks.

in oop perspective, have setcurrenttab in widget tabnav, in fp, in react, forbidden. entry door widget props. (refs evil, right ?).

this mean when tabbar receive user click, call callback in tabnav, , tabnav have call upper callback, defined in application. callback change application state, , set currentselectedtab, , passed through props tabnav.

the fact currentselectedtab must defined outside tabnav horrible. don't think ? looks me breaks component encapsulation. how ? flux ? same thing: currentselectedtab don't have defined in global state.

basically seems if functional, end-up giant state on top contains stuff belong widgets. encapsulation broken.

may explain me wrong here ?

having public method setting selected tab in imperative languages not different having prop meant same thing in react.

as feel need control property outside of component naturally becomes part of public contract. in react , preferably props, in common oop languages - public class methods/fields.

it not necessary react prop directly represent internal state though. totally ok use props initial values state. there special hook componentwillreceiveprops meant allow mutate encapsulated internal state when props changing.

hope clarify things bit.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -