mardi 6 juin 2017

React Component interface to methods

Q: Is there any way to provide an interface to access public methods from a given component?

Scenario: Imagine that I am buiding a Tabs component. It's located inside a parent component, called App:

<App>
    <Tabs />
    <button id="previous">Previous</button>
    <button id="next">Next</button>
</App>

As you can see there is two buttons that should interact with Tabs component, moving it to previous or next tab pane.

My implementation consists in keeping next() and previous() methods inside Tabs component:

class Tabs extends React.Component {
    next() {}
    previous() {}
}

Q: How can I bind next() and previous() methods to those buttons inside App component? Can I write some kind of interface exposed by Tabs component? What is the best and clean way to do that?

Aucun commentaire:

Enregistrer un commentaire