question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use ref for component

See original GitHub issue

Let’s say I use theme and I want to ref on SideBar like this

<SideBar ref={(el) => this.SideBar = el}>...</SideBar>

but this cannot be achieve. How to get ref for such component

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
GregoryPotdevincommented, Feb 8, 2017

@joemcelroy You can’t really do that as the SideBar has some filters as children, and hiding it would remove the children and the corresponding accessors. That could have unexpected results when running a new query and then showing the sidebar

What you do want, though, is to have all the prebuilt components that work like plain “div” elements with custom presets.

This is your current LayoutBuilder :

export const LayoutBuilder = (className)=> {
  return (props)=> (
      <div className={mixClasses(className, props.className)}>
        {props.children}
      </div>
  )
}

It doesn’t allow adding style or other attributes. What would be better is something like

export const LayoutBuilder = (baseClassName)=> {
  return ({className, children, ...props})=> (
      <div className={mixClasses(baseClassName, className) {...props}}>
        {props.children}
      </div>
  )
}

With something like this (untested) all the extra props get transfered to the div. So now you can add a ref or style={{display: 'none'}} (I wouldn’t recommend using the ref for that, it’s always better to stay in the React world)

0reactions
infacqcommented, Feb 14, 2017

So I have to produce extra div?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refs and the DOM - React
Refs are created using React.createRef() and attached to React elements via the ref attribute. Refs are commonly assigned to an instance property when...
Read more >
A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >
The Complete Guide to useRef() and Refs in React
useRef (initialValue) is a built-in React hook that accepts one argument as the initial value and returns a reference (aka ref). A reference...
Read more >
Using refs in React functional components (part 1) - useRef + ...
Refs are simply references to anything, like a DOM node, Javascript value, etc. To create a ref in a functional component we use...
Read more >
How to Use React Refs - Ross Bulat - Medium
Refs are created using React.createRef() , and are assigned to class properties. In the above example the ref is named myRef , which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found