5

I have a basic class:

export default class Foo extends Component{
 constructor(props){}
 woop(){
  somethingimportanthappening..
 }
}

in the other component I import it like this:

import Foo from './Foo'
export default class Component2 extends Component{
 constructor(props){}

 randomfunction(){
   Foo.woop() // FAILS
 }
}

How can I call the function "woop" from the other component (Component2)? Foo.woop() isn't working ..

2
  • OK, blame me for being old fashioned, if you will, but I've not played around with new ES6 stuff, so I'm not totally clear what this does. Yet, I think you're exporting a class Foo and woop() is an instance method. Hence, shouldn't you do foo = new Foo(); foo.woop()? Commented Sep 13, 2016 at 12:31
  • 1
    Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not"! Commented Sep 13, 2016 at 13:10

3 Answers 3

6

In React a component have an independent function, so in Foo component you define it, something like

export default class Foo extends Component{
  constructor(props){}
  woop(){
    somethingimportanthappening..
  }
  render() {
    <div>Class Foo</div>
  }
}

and when you want to use Foo component, you just import and use

<Foo />

and if you want to use a function in Foo component, you can define a reference to Foo

<Foo ref="foo" />

then use it by

import Foo from './Foo'
export default class Component2 extends Component{
  constructor(props){}

  randomfunction(){
    this.refs.foo.woop();
  }

  render() {
    return <Foo ref="foo" />
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Works perfectly for me :)
True, this works quite well, but only with a parental relationship.. what if there isn't any?
@dv3 if you inherit a class, you can call parent function through super.methodName(), maybe works :D
1

Since woop is not a static function you cannot Foo.woop()

You should instantiate a Foo instance as follows:

import Foo from './Foo'

export default class Component2 extends Component{
    private fooInstance: Foo;

    constructor(props){
      this.fooInstance = new Foo(props);
    }

    useFoo(){
      this.fooInstance.woop();
    }
}

Let me know if something is unclear :)

7 Comments

makes sense :) but it's throwing unexpected token on the 'private fooInstance: Foo;' line... confused
Try it without the private, it is just a recommendation. (assuming the unexpected token is the private and not something about the : Foo type)
'Foo is not a constructor'... lol is this something react-native specific?
updated my answer, your Foo class should have a constructor for instantiating a new instance of it
Thanks but it already had a constructor in it.. didn't really change it..
|
0

Creating an instance of a class to call a function seems wrong to me. I'm not sure but its like calling a function from different Activity inside another Activity in Android: You can't and mustn't do that.

Imo, the proper way to call a function of a different class to pass the function callback as props (if there is a parental relation). If there isn't any, triggering Events using react-native-simple-events is fairly simple.

Alternatively, you can use Redux which makes your app more robust. This may change and improve your overall app logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.