Code Monkey home page Code Monkey logo

Comments (2)

GabCores avatar GabCores commented on May 14, 2024 1

Thanks Dominik !! , I have solved the problem and learn in deep how that works, you made my day .

from hybrids.

smalluban avatar smalluban commented on May 14, 2024

Hi Gabriel! Welcome on board. The main concept of the hybrids is to separate side-effects from a declarative definition of the component.

The example from getting started section includes increaseCount() side effect called internally when a user clicks on button rendered by the component. In general, it should not be accessible outside of the component. It is internal side-effect connected to the component DOM element.

The declarative definition of the component includes count property, which is a public API of the component. It can be defined by the html attribute (statically in HTML) or manipulated dynamically by changing its value in JavaScript. User of the component can do what you are trying to do just by updating value of the "input":

const myElement = document.getElementsByTagName('simple-counter')[0];
myElement.count += 1; // This will update and re-render component

If your real component is more complex (and you really want to expose that side effect programmatically), and your "side-effect" is not a simple as increasing input value, you have two options. Firstly, you can expose your side effect function to the user. A mentioned example is doing that - it exports increaseCount function, so the user might use it:

import { increaseCount } from './simple-counter';

const myElement = document.getElementsByTagName('simple-counter')[0];
increaseCount(myElement); // does the same as in above example

The second option is to provide methods directly on your component definition. However, It is not recommended solution - it tights the definition with side effects making the component harder to test, and encourage to create complex logic inside of the component. To do so you should create a getter property, which returns a function:

const MyElement = {
   doSomething: (host) => (param) => {
      // do something with a host
      host.count = param * 10;
   },
   count: 0,
};

The returned function will be created once, as a getter does not use host properties (cache will save returned function and use it for next invocations).

from hybrids.

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.