Function components cannot have refs. Did you mean to use React.forwardRef()?
See original GitHub issue//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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It works here: https://snack.expo.io/@jeremdsgn/modalize-error
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.
Thanks for the comment, glad it works!