12

My first component is as follow:

const hellos = ['Hola', 'Salut', 'Hallo', 'Ciao', 'Ahoj', 'Annyeong-haseyo', 'Aloha', 'Howdy', 'Ni Hao', 'Konnichiwa']

export class Welcome extends Component {

    constructor(props) {
        super(props);
        this.state = {
            errors: []
        };
    }

    sayHello = function() {
        return hellos[Math.floor((Math.random()*hellos.length))];
    }

    render() {
        return (
            <div className="Welcome">

            </div>
        );
    }
}

I want to be able to call sayHello() from another component. All answers I've seen so far talk about parent and child relationships but in this case the two components don't have any relationship. I thought of something like this but it doesn't do the job:

import { Welcome } from './Welcome'

export const Life = () => (
    <div className="Life">
      <p>{ Welcome.sayHello() }</p>
    </div>
)

I would like to get a random element of the hellos array printed in Life.

7
  • 1
    This doesn't seem like the right design pattern... Commented May 21, 2017 at 21:00
  • I forgot a line with the method call. Commented May 21, 2017 at 21:01
  • So what do you expect to happen in the Life component? Should it show the Welcome component at all? Because your current code will not. Commented May 21, 2017 at 21:01
  • @ocram Well, the reason why it's not working is because sayHello is a method of an instance, not a static function. You would have to create an instance of Welcome via creating a Welcome element and using refs or making it static. Anyways, this seems like the wrong decision. Commented May 21, 2017 at 21:03
  • It has to be parent child relationship. React works on props and states architecture. If you want to use sayHello in other component, then you will have to pass sayHello as prop to the other component from this component. Commented May 21, 2017 at 21:03

1 Answer 1

11

There are a number of ways you could achieve this:

You can do this by creating the sayHello function and using it simply as a named-function.

hello.js

const hellos = ['Hola', 'Salut', 'Hallo', 'Ciao', 'Ahoj', 'Annyeong-haseyo', 'Aloha', 'Howdy', 'Ni Hao', 'Konnichiwa'];

const sayHello = function() {
    return hellos[Math.floor((Math.random()*hellos.length))];
};

export { sayHello };

Then you can import into which ever component you wish to share the functionality:

import { sayHello } from './hello';

class CompA extends React.Component {
    render() {
        return <span>{sayHello()}</span>;
    }
}

class CompB extends React.Component {
    render() {
        return <span>{sayHello()}</span>;
    }
}

render(<span>
        <CompA />
        <CompB />
    </span>, document.querySelector('#app'));

Created a https://www.webpackbin.com/bins/-KkgrwrMGePG4ixI0EKd

Another way, is to simply define your sayHello Function as static.

static sayHello() {
    return hellos[Math.floor((Math.random()*hellos.length))];
}
Sign up to request clarification or add additional context in comments.

1 Comment

or you can use a ref for a component and call a method on it.

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.