question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function components cannot have refs. Did you mean to use React.forwardRef()?

See original GitHub issue
Screenshot 2020-05-06 at 6 14 14 PM
//Code that's giving Error
import React, {useRef} from 'react';
import {View, Text, Button} from 'react-native';
//Modalize
import {Modalize} from 'react-native-modalize';
// Design System
import styled from 'styled-components';
// Colors
import colorSchemes from '../design_system/colors';
import {useDarkModeContext} from 'react-native-dark-mode';
// Custom Components
import Tweet from '../components/Tweet';
import Search from '../components/Search';

function DiscoverScreen({navigation}) {
  const colorScheme = useDarkModeContext();
  const colors = colorSchemes[colorScheme] || colorSchemes.light;

  // Styled Component
  const Screen = styled.View`
    flex: 1;
    background-color: ${colors.background};
  `;

  //Modalize
  const modalizeRef = useRef < Modalize > null;

  const onOpen = () => {
    modalizeRef.current?.open();
  };

  return (
    <>
      <Screen>
        <Tweet onPress={onOpen} />
      </Screen>

      <Modalize
        scrollViewProps={{showsVerticalScrollIndicator: false}}
        snapPoint={300}
        ref={modalizeRef}>
        <View style={{height:500, backgroundColor: 'blue'}} />
      </Modalize>
    </>
  );
}

export default DiscoverScreen;

Tweet is a custom Component which has TouchableWithoutFeedback for accepting onPress

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jeremybarbetcommented, May 6, 2020

It works here: https://snack.expo.io/@jeremdsgn/modalize-error

const modalizeRef = useRef<Modalize>(null);

const onOpen = () => {
  modalizeRef.current?.open();
};

This syntax is using TypeScript. If you are running under JavaScript, like it is the case under the snack repro you provided, you need to remove the typescript syntax otherwise it won’t understand it.

const modalizeRef = useRef(null);

const onOpen = () => {
  if (modalizeRef.current) {
    modalizeRef.current.open();
  }
};
2reactions
jeremybarbetcommented, May 6, 2020

Thanks for the comment, glad it works!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function components cannot have refs. Did you mean to use ...
I 'm using 16.9, but working with existing React Native code that was written with 16.3.1. It uses the old style with ref=......
Read more >
Function components cannot have string refs in React
The error "Function components cannot have string refs" occurs when we use a string as a ref in a function component. To solve...
Read more >
Forwarding Refs - React
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >
ref prop should not be used on React function components
This rule applies when a ref prop is specified on a React function component. The ref prop is used to provide a reference...
Read more >
warning: function components cannot be given refs. attempts ...
Attempts to access this ref will fail. Did you mean to use React. forwardRef ()? 问题原因. 因为原来父组件通过 ref 获取了子组件的节点(子组件是class创建 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found