Coder Social home page Coder Social logo

components as hooks about spect HOT 7 CLOSED

dy avatar dy commented on September 1, 2024
components as hooks

from spect.

Comments (7)

dy avatar dy commented on September 1, 2024

Essentially writing hook and writing a component is almost the same:

function Form (props) {
return JSX
}

function useForm (props, deps) {
return JSX
}

Component is just a form of hook, returning JSX.

from spect.

dy avatar dy commented on September 1, 2024

! mod can directly depend on it's props, so that is called only when it's props changed

@mod
function content (el, props) {
  
  @mod
  let result = fx(subProps)
  
}

from spect.

dy avatar dy commented on September 1, 2024

! portals as

function App(props) {
  render(<jsx.../>, target)
  render(<jsx.../>, root)
}

same as

render(<jsx1>  ...<jsx2/>   </jsx1>)

each nested tag is mounted on the parent container.
The structure tries to repeat itself, to make parent match to the part of code

from spect.

dy avatar dy commented on September 1, 2024

useEffect without dependencies is the same as useEffect(fn, [...props]). Effect with blank deps should be equivalent to run-once, not run-always.

This way, hooks are just conditional execution decorators over functions

@deps(props)
fx(){}

or more generally

@condition(props => true|false)
fx() {}

in other words

if (propsChanged)
fx(){}

from spect.

dy avatar dy commented on September 1, 2024

Looking at https://github.com/dy/mods/blob/master/hooks-components.jsx, the pattern emerges.
Since component is a set of connections/subscriptions to model, triggering update; and update is a set of conditional reactions/effects - that can be organized as a class with decorators, not extending some generic class but just using entries.

Inspiration: https://github.com/tc39/proposal-decorators#tracked

class Mod {
  // useState equivalent / @initialize
  @state value = initial

  @query
  query,
  
  @history
  history,

  // created once, therefore a sense to have it as decorator
  @async(api.getCatalog)
  catalog = api.getCatalog,

  @async
  categories = api.getCategories,

  @deps(catalog, )
  products,
  
  // assigning new selectedProduct triggers rerendering
  @state(null)
  selectedProduct,
  
  // rerender modal whenever any of the props change as an effect
  @deps(props)
  @mount(modal-root)
  selectedModal = (props) => <dialog {...props} />
  
  // reinit css whenever any of the props change
  @css(a, b, c)
  rootStyle (a, b, c) { return `.class-1 {} .class2 {}` }

  // main render
  render (props) {}
}

from spect.

dy avatar dy commented on September 1, 2024

ok, waiting for the decorators to come.
Better focus on mods- JSX now, there isn't going to be any updates.

!decorators for JSX

<div @provide() @css() >
</div>

from spect.

dy avatar dy commented on September 1, 2024

Hooks are not components, hooks are parts of the components/custom elements behavior/effects.

from spect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.