Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref warning when using component for the content #134

Open
Saikedo opened this issue Dec 20, 2021 · 3 comments
Open

Ref warning when using component for the content #134

Saikedo opened this issue Dec 20, 2021 · 3 comments

Comments

@Saikedo
Copy link
Contributor

Saikedo commented Dec 20, 2021

I am using the library for my React-Native-Web project and when I try to use a component for the content of the toolTip, the Chrome console throws an error when the ToolTip appears for the first time. For this code

const ToolTipContent = () => {
    return (
        <View style={{ width: 100, height: 100, backgroundColor: '#ff0000' }} />
    );
};

<Tooltip
    isVisible={isToolTipVisible}
    content={<ToolTipContent />}
    placement="top"
    onClose={() => setIsToolTipVisible(false)}
>
    <TouchableHighlight
        style={{ width: 100, height: 40, backgroundColor: '#0000ff', borderRadius: 8 }}
        onPress={() => setIsToolTipVisible(true)}
    >
        <Text>Press me</Text>
    </TouchableHighlight>
</Tooltip>

It shows an error saying Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Check the render method of 'TouchableWithoutFeedback'.

Hovewer if I replace the content with just <View style={{ width: 100, height: 100, backgroundColor: '#ff0000' }} /> it works fine. I tried just remporarily replacing the TouchableWithoutFeedback components inside the react-native-walkthrough-tooltip library with TouchableOpacity and the error was gone.

Any ideas on how we can avoid this warning currently? Should I make a push and change the TouchableWithoutFeedback component with TouchableOpacity and set ActiveOpacity to 1?

Thank you.

@Saikedo
Copy link
Contributor Author

Saikedo commented Dec 20, 2021

Wrapping the component in React.forwardRef also seems to fix the issue for some reason.

Basically change ToolTipContent to

const ToolTipContent = React.forwardRef(() => {
  return (
    <View style={{ width: 100, height: 100, backgroundColor: '#ff0000' }} />
  );
});

So I guess another solution will be to do the following inside react-native-walkthrough-tooltip

Declare a function like this
const GetContentAsForwardRef = React.forwardRef(() => this.props.content);

And then replace this

<TouchableWithoutFeedback
  onPress={onPressContent}
  accessible={this.props.accessible}
>
  {this.props.content}
</TouchableWithoutFeedback>

with this

<TouchableWithoutFeedback
  onPress={onPressContent}
  accessible={this.props.accessible}
>
  <GetContentForwardRef/>
</TouchableWithoutFeedback>

I can make a push addressing the issue by either method 1 that I mentioned above or with this ForwardRef method mentioned here. Please let me know if that will be helpful.

@gregfenton
Copy link

Seems like a reasonable approach, but I have not played with react-native-web enough to be sure.

@Saikedo
Copy link
Contributor Author

Saikedo commented Jan 11, 2022

@gregfenton I realized that this is not really specific to React Native Web actually. Also it seems like the correct way to fix this would be to do the const ToolTipContent = React.forwardRef... solution instead of what I proposed originaly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants