Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

hooks in export default class App extends React.Component

I'm trying to build a React Native app and re-use some of the principles from reactnativeschool but I'm struggling with the Hook:

  const { scrollEnabled, setScrollEnabled } = useState(false);

placed in:

export default class App extends React.Component {

It i originally placed in export default () => { but I have to change that for other reasons.

Depending where I place it, I either get unexpected token or Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.

I have tried moving it around and refactoring but no luck. Can you find out what is wrong or where it should be placed?

I'll try to give a skeleton of my code below:

import React, { useState, useContext } from "react";
import {  View,  StyleSheet,  StatusBar,  Dimensions,  Text,  ScrollView,  TouchableOpacity} from "react-native";

const screen = Dimensions.get("window");

const formatNumber = (number) => `0${number}`.slice(-2);

const getElapsed = (time) => {
  return {
    hours: formatNumber(hours)
  };
};

const { scrollEnabled, setScrollEnabled } = useState(false); // Issue with this line. Tried to move it in different places

export default class App extends React.Component {
  interval = null;

  constructor(props) {
    super(props);

    this.state = {
      elapsedMilliseconds: 0
    };
  }

  stop = () => {
    clearInterval(this.interval);
    this.interval = null;
    this.setState({
      isRunning: false,
    });
  };

  render() {
    const { hours, minutes, seconds, milliseconds } = getElapsed(
      this.state.elapsedMilliseconds
    );

    return (

      <View style={styles.container}>
        <ScrollView>
          <View style={styles.content}>
            // Stuff
          </View>

        </ScrollView>
      </View>
    );
  }
}
question from:https://stackoverflow.com/questions/65864953/hooks-in-export-default-class-app-extends-react-component

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

first, you can only use hook inside functional component, not in class component or outside component.

if you are using class component, just use state and setState instead of hook

like:

export default class App extends React.Component {
   constructor(props){
   this.state = {
      scrollEnabled: true
   }
   }

   stop = () =>{
    this.setState({scrollEnabled: false})
   }

   render(){
      return (
      <View style={styles.container}>
        <ScrollView>
          <View style={styles.content}>
            // Stuff
          </View>

        </ScrollView>
      </View>
      )
   }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...