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

after render hook? #4

Open
leeoniya opened this issue Oct 21, 2015 · 10 comments
Open

after render hook? #4

leeoniya opened this issue Oct 21, 2015 · 10 comments

Comments

@leeoniya
Copy link

Hi,

How would you suggest to handle a case when you need to interact programatically with an element or component after it is rendered. For example rendering a <textarea> with a value and setting the cursor position or selection range within it after it has been rendered?

Is there a post-render hook that provides a reference to the element's node to allow for this?

thanks!

@leeoniya leeoniya changed the title after render hook after render hook? Oct 21, 2015
@creationix
Copy link
Owner

What's the use case? Rendering is synchronous.

@leeoniya
Copy link
Author

sorry premature submit, read above.

async rendering is another issue i will open later :), like returning a Promise from render or a [Promise, placeholder]

@leeoniya
Copy link
Author

perhaps use the same strategy as refs. and pass an object of tagged nodes to a pluggable afterRender callback.

EDIT: if this callback fires on both initial render and refresh, probably passing in isInitial would be useful as well when invoked

@leeoniya
Copy link
Author

another use-case is setting .value on a <select> node instead of re-rendering an option[selected] every time.

another is setting a form element's focus.

@creationix
Copy link
Owner

In dombuilder (the previous generation to domchanger), I had a special $ property that was a function to call when a node was rendered to the dom https://github.com/creationix/dombuilder/blob/master/dombuilder.js#L71-L72

I vaguely remember something being done here, but can't find it. The raw "el" node type was added when I was using codemirror in an app. (See http://desktop.creationix.com/ and https://github.com/creationix/tedit/blob/master/src-minimal/ui/code-mirror-editor.js#L18-L31)

@leeoniya
Copy link
Author

coincedentally, i'm writing a toy in-browser IDE with tabbed nav, session restore, status bar and codemirror to test domchanger. this involves restoring codemirror selections, cursor pos, history state. file contents are loaded async from the server, so a way to do deferred render via Promises will need to be figured out.

a per-element afterRender(el, isInitial) callback would be ideal, but a single callback at component level would be okay too...maybe even both.

creationix added a commit that referenced this issue Oct 21, 2015
@creationix
Copy link
Owner

I think this afterRefresh hook will help. I can also add a per-element hook for rendered elements like dombuilder had if that is helpful.

I'm still not sure why domchanger should have to know anything about the promises for fetching your data. Simply call refresh or emit an event when your promise finally gets fulfilled.

@leeoniya
Copy link
Author

yes, this works well.

i think isInitial would be useful to know since render is called as both a pseudo-constructor to inject params into the component's closure as well as a refresh re-renderer.

EDIT: i may be missing something here, so please clarify if any of this is incorrect
EDIT 2: extra stuff in this comment moved to own issue (#5)

@leeoniya
Copy link
Author

i do think that having an afterRefresh(isInitial) per element would be useful and would make for cleaner strings since tagging would no longer be compulsory and the setup would be more declarative and in-place.

@creationix
Copy link
Owner

I have wished a few times that the component had some sort of initialization parameters apart from the render data. I'll have to think about to fit this in.

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