I've been trying to figure out how to do this in rum:
class MyComponent extends Component {
componentDidMount() {
this.node.scrollIntoView();
}
render() {
return <div ref={node => this.node = node} />
}
}
Anyone knows?I just don't understand how I'm supposed to make use of ref
This is the best I got, is that the idiomatic rum way?
(rum/defcs MyComponent <
{:class-properties {"node" (rum/create-ref)}
:did-mount
(fn[state]
(.scrollIntoView (.-node (:rum/react-component state)))
state)}
[state]
[:div {:ref (.-node (:rum/react-component state))}])
(rum/defc MyComponent []
(let [ref (rum/use-ref nil)]
(rum/use-effect! #(.scrollIntoView (rum/deref ref))
[])
[:div {:ref ref}]))
the above should work with hooks
Hum, I'd need to switch fully to hooks though for that. Is what I did for class based components the best it gets?
Not fully, just for that component by using defc
Hum, that's true, I could explore that option, though I think I might be using other mixins. But I guess I can learn hooks and move those to hooks as well
@didibus Untested
did-mount
would probably need to return rum-state.
(fn [state]
(.scrollIntoView (rum/dom-node state))
state)
dom-node is deprecated though. I was trying to use the suggested replacement. Which based on the react documentation, is what I pasted above, that is to use a ref