首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无效的挂钩调用挂钩只能在函数组件的主体内调用

无效的挂钩调用挂钩只能在函数组件的主体内调用
EN

Stack Overflow用户
提问于 2020-09-04 12:43:09
回答 3查看 165关注 0票数 0

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

代码语言:javascript
复制
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>
        );
    }
}
EN

回答 3

Stack Overflow用户

发布于 2020-09-04 12:53:39

你应该只在功能组件中使用钩子。阅读文档here

代码语言:javascript
复制
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>
  );

}
票数 1
EN

Stack Overflow用户

发布于 2020-09-04 17:09:56

据我所知你想要一个裁判。有两种方法

  1. 使用createRef在构造函数中声明它(不需要导入)

构造函数(Props){ this.modalizeRef = React.createRef();}

  1. 在组件调用中指定它

<模块化ref={(ref) => { this.modalizeRef = ref;}} />

票数 1
EN

Stack Overflow用户

发布于 2020-09-04 20:55:02

我设法通过使用refs callback解决了这个问题

代码语言:javascript
复制
  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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63734969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档