我目前正在使用Modalize库实现一个模型。我对请求使用了Hooks,但只要我要调用Modalize组件,它就会返回以下错误。我花了很多时间,还没弄清楚问题出在哪里

import React, { useRef } from 'react';
import { Modalize } from 'react-native-modalize';
export default class MyScreen extends React.Component {
render() {
const modalizeRef = useRef(null);
function onOpen() {
modalizeRef.current?.open();
}
return (
<View>
<TouchableOpacity onPress={onOpen}>
<Text>click me</Text>
</TouchableOpacity>
<Modalize
ref={modalizeRef}
snapPoint={180}
>
<View
style={{
flex: 1,
height: 100,
flexDirection: 'row',
}}>
<TouchableOpacity style={[style.btnTest, { backgroundColor: '#29ae19' }]}>
Button 01
</TouchableOpacity>
<TouchableOpacity style={[style.btnTest, { backgroundColor: '#ff0000' }]}>
Button 02
</TouchableOpacity>
</View>
</Modalize>
</View>
);
}
}发布于 2020-09-04 12:53:39
你应该只在功能组件中使用钩子。阅读文档here
export default function MyScreen() {
const modalizeRef = useRef(null);
function onOpen() {
modalizeRef.current?.open();
}
return (
<View>
<TouchableOpacity onPress={onOpen}>
<Text>click me</Text>
</TouchableOpacity>
<Modalize
ref={modalizeRef}
snapPoint={180}
>
<View
style={{
flex: 1,
height: 100,
flexDirection: 'row',
}}>
<TouchableOpacity style={[style.btnTest, { backgroundColor: '#29ae19' }]}>
Button 01
</TouchableOpacity>
<TouchableOpacity style={[style.btnTest, { backgroundColor: '#ff0000' }]}>
Button 02
</TouchableOpacity>
</View>
</Modalize>
</View>
);
}发布于 2020-09-04 17:09:56
据我所知你想要一个裁判。有两种方法
构造函数(Props){ this.modalizeRef = React.createRef();}
<模块化ref={(ref) => { this.modalizeRef = ref;}} />
发布于 2020-09-04 20:55:02
我设法通过使用refs callback解决了这个问题
export default class MyScreen extends React.Component {
constructor(props) {
super(props);
this.modalizeRef = React.createRef();
}
onOpen() {
this.modalizeRef.current?.open();
}
}
<View>
<TouchableOpacity onPress={this.onOpen.bind(this)}>
<Text>click me</Text>
</TouchableOpacity>
<Modalize
ref={this.modalizeRef}
snapPoint={180}
>
<View
style={{
flex: 1,
height: 100,
flexDirection: 'row',
}}>
<TouchableOpacity style={[style.btnTest, { backgroundColor: '#29ae19' }]}>
Button 01
</TouchableOpacity>
<TouchableOpacity style={[style.btnTest, { backgroundColor: '#ff0000' }]}>
Button 02
</TouchableOpacity>
</View>
</Modalize>
</View>https://stackoverflow.com/questions/63734969
复制相似问题