realar-project / realar

5 kB Advanced state manager for React
MIT License
44 stars 0 forks source link

low: add hook support to scoped instances #93

Closed betula closed 3 years ago

betula commented 3 years ago

const scope_factory = () => {
  const v = value(0);
  hook(() => {
    const item = useExternalSelector(external_selector);
    v(item);
  });

return v;
};

const Child = () => {
  const val = useValue(useScoped(scope_factory));
  return (
    <span>{val}</span>
  )
}

const Root = () => {
  return (
    <ExternalContext>
      <Scope>
        <Child />
      </Scope>
    </ExternalContext>
  )
}
betula commented 3 years ago

Not possible in React context scope in contrast with local logic. Because local logic executed in React component who called factory. But in React context scope case, logic should be executed in the Scope component, not in the current execution context.