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
Add Ref Feature #2805
Add Ref Feature #2805
Conversation
Add the ref attribute. When vnode contains the ref attribute, vnode assigns its refrence to `ref.current` when it is created. This is similar to ref in react. This makes it easy for developers to get a specific vnode refrence when using chain method to create the vnode tree.
Hi @clysto, you can get this functionality in current Mithril without any extra APIs. Mithril function components pre-date hooks-aware React components and behave differently: React function components execute on each draw and as a result need special APIs to retain references. Mithril function components execute only the first time each component instance is created, and the returned The advantage of the Mithril method is that everything in the component function scope is persistent for the lifetime of that component, which means we can declare references and initialisation code at setup, and mutate or reassign those references later on. The way to achieve the same functionality as your code sample is to simply declare an unassigned function HomePage() {
let input // 1
const handleClick = () => {
console.log(input.dom.value); // 3
};
return {
view: () =>
m('div', [
input = m('input', { placeholder: 'name' }), // 2
m('button', { onclick: handleClick }, 'OK')
]),
};
}
m.mount(document.body, HomePage); Here's a sandbox for the sample above
|
Wow, it is amazing. But how to do the same thing in JSX? |
Like this: function HomePage() {
let input // 1
const handleClick = () => {
console.log(input.dom.value);
};
return {
view: () =>
<div>
{[
input = <input placeholder="name" />,
<button onclick={handleClick}>
OK
</button>
]}
</div>
};
}
m.mount(document.body, HomePage); |
Yes, it seems that we don't need any extra api to implement this feature. But it doesn't look so elegant in JSX. As you said: mithril function components execute only the first time each component instance is created, it can be implemented through simple assignment statements. Maybe I can use Babel to implement the same thing at compile time? |
@clysto I don't believe there's a pre-made transform, but it should be pretty easy to roll a custom Babel plugin to do it. (Tip: start with this handbook if you aren't familiar.) All you really need to do is find Do note that JSX elements are expressions. They aren't magical syntax constructs. So don't be afraid to treat them as such. |
Description
Add the ref attribute. When vnode contains the ref attribute, vnode assigns its refrence to
ref.current
when it is created.This is similar to ref in react.
Motivation and Context
This makes it easy for developers to get a specific vnode refrence when using chain method to create the vnode tree.
For example:
How Has This Been Tested?
Types of changes
Checklist:
docs/changelog.md