1

I'm developing a component to publish it in npm, but I'd like to call my component using a method instead of a tag.

Example:

myComponent.js

import React from 'react'
import { View, Text } from 'react-native'

export const showComponent = () => {
    // this would be the function that I user to call my component down
}

const myComponent = (props) => {
    return(
        <View>
            <Text>Oi</Text>
        </View>
    )
}

App.js

import React from 'react'
import { View, Text, TouchableOpacity } from 'react-native'
import { showComponent } from 'my-component'

const App = () => {
    return(
        <View>
            <TouchableOpacity onPress={() => showComponent()}>
                <Text>Home</Text>
            </TouchableOpacity>
        </View>
    )
}

export defaul App

the idea is that when calling the showComponent function I show my component, and when I call, for example, the hide function, I close my component.

2
  • Why are you not passing a prop to your component to hide/show it ? Commented May 9, 2019 at 17:57
  • I would like to call it within the redux action, so I thought of the` method` (just like nativebase does with Toast), it will be a popup component Commented May 9, 2019 at 18:00

3 Answers 3

1

You can do it using a single class export:

import * as React from 'react';

export default class MyComponent extends React.Component {
  state = {
    isOpen: false,
  };

  open = () => {
    this.setState({ isOpen: true });
  };

  close = () => {
    this.setState({ isOpen: true });
  };

  render() {
    const { isOpen } = this.state;

    return !isOpen ? null : (
      <View>
        <Text>Oi</Text>
      </View>
    );
  }
}

And you use it like so:

<MyComponent ref={(x) => this.myComponent = x)} />

And you open it like so:

this.myComponent.open();
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, i think that this that i wanted. i gonna go try.
0

I see in a comment above you want to call the component with a redux action, so you should call your redux action in that on click, but the component you want to show/hide needs to be linked to a redux state variable. Then in your jsx you'd have:

        <View>
            <TouchableOpacity onPress={() => showComponent()}>
                <Text>Home</Text>
            </TouchableOpacity>
            {reduxBoolean && <MyComponent />}
        </View>

Comments

0
import React from 'react'
import { View, Text} from 'react-native'
const example = (props) => {
    return (
        <View>
            <Text>Hello</Text>
        </View>
    )
}

// props

import React from 'react'
import { View, Text} from 'react-native'

const examples = () => {
    return(
        <View>
            <Text><example/></Text>
        </View>    
    )
}

and print is : Hello

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.