expose component methods
See original GitHub issueDo 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:
- Created 6 years ago
- Reactions:1
- Comments:12 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Typically in React you solve this by providing a declarative API in props instead. For example instead of
goToPage()
method you would implement acurrentPage
numeric prop. This way it can be controlled with state by any component higher up the tree.