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

Referencing DOM nodes on demand #85

Closed
jcfranco opened this issue Jan 9, 2017 · 2 comments
Closed

Referencing DOM nodes on demand #85

jcfranco opened this issue Jan 9, 2017 · 2 comments

Comments

@jcfranco
Copy link
Contributor

jcfranco commented Jan 9, 2017

What's the recommended approach to referencing DOM nodes on demand (akin to refs in React)?

In this toy snippet (codepen here), the node of interest is stored whenever it is created and referenced afterwards.

var shared = { node: null };

function focusNode() {
  shared.node.focus();
}  

function storeNode(node) {
  shared.node = node;
}

var form = {
  renderMaquette: function() {
    return h('div', [
      h('textarea', { afterCreate: storeNode }),
      h('button', { onclick: focusNode }, 'focus')
    ]);
  }
};

Is this a reasonable pattern or is there a better way?

@RickHoving
Copy link
Contributor

That is the way we do things :)
Also note that in most events like the onclick you also can access the DOM nodes.
This is useful when you want to access that particular node, but I think its not useful in your particular example.

@jcfranco
Copy link
Contributor Author

Thanks for the info! It may be worth adding an entry about node-reference patterns to the cookbook.

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

No branches or pull requests

2 participants