Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update methods.md #1482

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Update methods.md #1482

wants to merge 2 commits into from

Conversation

msqaddura
Copy link

@msqaddura msqaddura commented Sep 17, 2024

it seems what we want to say is that; it is callable from outside, not directly from the element (component).
though they can be called directly but the following sentence "i.e. they are intended to be callable from the outside!"
states otherwise

fix potentially misguiding instruction
@msqaddura msqaddura requested a review from a team as a code owner September 17, 2024 11:24
@msqaddura msqaddura requested review from christian-bromann and gnbm and removed request for a team September 17, 2024 11:24
Copy link

vercel bot commented Sep 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
stencil-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 11:38am

@christian-bromann
Copy link
Member

@msqaddura mhm 🤔 I am not sure if this makes sense, do you have a concrete example?

@msqaddura
Copy link
Author

msqaddura commented Sep 27, 2024

maybe first let me explain myself. to be sure i understand it correctly

  1. so the point of @Method decorator is to give access to components, from another component. and that is when we use document.querySelector. is that right?

  2. a component doesnt need @Method to access its own functions, we can use private func... or public func...

  3. the statement below might be misleading given the example is not related to the statement first part says it can be called from within, the i.e given states that it is called from outside

decorator can be called directly from the element, i.e. they are intended to be callable from the outside!

https://stackblitz.com/edit/stencil-demo-itm9py?file=src%2Fwith-method.tsx,src%2Findex.html

import { h, Component, Host, State, Method } from '@stencil/core';

@Component({
  tag: 'with-method',
  shadow: true,
})
export class WithMethod {
  @State() text = '';
  @State() counter = 0;

  @Method() // Method is needed to expose the function
  async setTextFromOutside(text: string) {
    this.text = text;
  }

  // same functionality can be achieved without @Method
  private setTextWithin(text: string) {
    this.text = text;
  }
  onClick() {
    this.counter += 1;
    this.setTextWithin('Called Within!'); // this call can be private
    this.text = 'Called Within!'; //we can access name from here, no need for setText
  }

  render() {
    return (
      <Host>
        <button onClick={() => this.onClick()}>Click me</button>
      </Host>
    );
  }
}

@Component({
  tag: 'method-caller',
  shadow: true,
})
export class MethodCaller {
  onClick() {
    const target = document.querySelector('with-method')!;

    target.setTextFromOutside('Called Outside!');
    // target.setTextWithin("Error"); //this is not going to work as it is not exposed via @Method()
  }
  render() {
    return (
      <Host>
        <button onClick={() => this.onClick()}>Click me</button>
      </Host>
    );
  }
}

Overall, maybe it is not that important but would be good to explain the intent of the @Method that it is meant to be called from outside the component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants