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.

expose component methods

See original GitHub issue

Do you want to request a feature or report a bug?

request a feature

What is the current behavior?

methods of components cannot be exposed

What is the expected behavior?

I would like to be able to expose methods of a component so it can be used from a container component, it can be done through an attribute similar to ‘ref’, like for example ‘expose’

For example, imagine a have a car component and a wheel component, the car would like to rotate the wheels using a method that the wheels expose to the cars

Something like:

class Car extends Component {
  constructor(props) {
    super(props);
  }
  rotateWheels() {
    this.wheels.rotate(32);
  }
  render() {
    return (
      <Body/>
      <Wheels expose={ ({ rotate }) => this.wheels.rotate = rotate }/>
    );
  }
}

class Wheels extends Component {
  constructor(props) {
    super(props);
    this.state = {
       wheelRotation: 0;
    };
  }
  rotateWheels(angle) {
    this.setState({wheelRotation: angle}); 
  }
  render() {
    const wheelStyle = {transform: `rotate(${this.state.wheelRotation}deg)`} 
    return (
      <div className="wheel" style={wheelStyle}/>
      <div className="wheel" style={wheelStyle}/>
      <div className="wheel" style={wheelStyle}/>
      <div className="wheel" style={wheelStyle}/>
    );
  }
}

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

all versions, all browsers

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Jan 30, 2018

Instance ref lets you access instance methods (this is what it’s for). Still, the right way to implement this in React in most cases is to provide a declarative API as mentioned above.

1reaction
gaearoncommented, Jan 28, 2018

it’s the responsibility of the carousel component. It’s dynamically calculated based on the items array and the carrousel width and it has a nextPage() method, and a previousPage(), and a goToPage() that modify the current page state parameter from the carousel

Typically in React you solve this by providing a declarative API in props instead. For example instead of goToPage() method you would implement a currentPage numeric prop. This way it can be controlled with state by any component higher up the tree.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose Component Functions | React
There's another (uncommon) way of communicating between components: simply expose a method on the child component for the parent to call.
Read more >
Exposing functions from React Functional Components
Using the useImperativeHandle hook in functional components we can expose functions that the parent of our component can call to perform actions ...
Read more >
Understanding Vue 3's "expose" - Vue Mastery
The new expose method is very intuitive and easy to implement in our components. It clears up a couple of very important composition...
Read more >
how to expose component methods in render function in vue3 ...
Component instance can be extended by using expose , this is useful for cases where setup return value is already render function:
Read more >
How do you expose component's methods? #1641 - GitHub
Steps to reproduce. Suppose the component uses render function and has following methods: const Cmp = defineComponent({ setup () { const active ...
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