Javascript React native:我无法将状态函数转换为挂钩

Javascript React native:我无法将状态函数转换为挂钩,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我在尝试将这个状态函数或代码集转换为钩子时遇到困难,因为我相信钩子更整洁、更直接。代码如下: 状态={ 捕获:[], //设置默认情况下关闭闪光灯 flashMode:Camera.Constants.flashMode.off, 捕获:空, //默认情况下,启动后摄像头 cameraType:Camera.Constants.Type.back, }; setFlashMode=(flashMode)=>this.setState({flashMode}); setCameraType=(c

我在尝试将这个状态函数或代码集转换为钩子时遇到困难,因为我相信钩子更整洁、更直接。代码如下:

状态={
捕获:[],
//设置默认情况下关闭闪光灯
flashMode:Camera.Constants.flashMode.off,
捕获:空,
//默认情况下,启动后摄像头
cameraType:Camera.Constants.Type.back,
};
setFlashMode=(flashMode)=>this.setState({flashMode});
setCameraType=(cameraType)=>this.setState({cameraType});
handleCaptureIn=()=>this.setState({capturing:true});
handleCaptureOut=()=>{
if(this.state.capting)
这个.camera.stopRecording();
};
handleShortCapture=async()=>{
const photoData=wait this.camera.takePictureAsync();
this.setState({capturing:false,captures:[photoData,…this.state.captures]})
};
handleLongCapture=async()=>{
setTimeout(()=>this.state.capturing&&this.camera.stopRecording(),20*1000);
const videoData=wait this.camera.recordAsync();

this.setState({capting:false,captures:[videoData,…this.state.captures]});
有三种方法

第一种是使用钩子,这可能有点过分了

第二个是有一个状态对象,它不是很优雅,但看起来像这样:

function FunctionName() {
    // set the state with it's default values
    const [state, setState] = useState({
        captures: [],
        flashMode: Camera.Constants.FlashMode.off,
        capturing: null,
        cameraType: Camera.Constants.Type.back,
    });

        // we spread the previous state, and then set the value we want to change.
        setFlashMode = (flashMode) => this.setState({...state, flashMode});
        setCameraType = (cameraType) => this.setState({...state, cameraType});
        handleCaptureIn = () => this.setState({...state, capturing: true});
    
        handleCaptureOut = () => {
            if (this.state.capturing)
                this.camera.stopRecording();
        };
    
        handleShortCapture = async () => {
            const photoData = await this.camera.takePictureAsync();
            this.setState({...state, capturing: false, captures: [...this.state.captures, photoData]})
            
        };
        
        handleLongCapture = async () => {
            setTimeout(() => this.state.capturing && this.camera.stopRecording(), 20*1000);
            const videoData = await this.camera.recordAsync();
            this.setState({...state, capturing: false, captures: [...this.state.captures, videoData]});
        }

    return <></>;
}
函数FunctionName(){
//使用默认值设置状态
常量[状态,设置状态]=使用状态({
捕获:[],
flashMode:Camera.Constants.flashMode.off,
捕获:空,
cameraType:Camera.Constants.Type.back,
});
//我们传播上一个状态,然后设置要更改的值。
setFlashMode=(flashMode)=>this.setState({…状态,flashMode});
setCameraType=(cameraType)=>this.setState({…state,cameraType});
handleCaptureIn=()=>this.setState({…状态,捕获:true});
handleCaptureOut=()=>{
if(this.state.capting)
这个.camera.stopRecording();
};
handleShortCapture=async()=>{
const photoData=wait this.camera.takePictureAsync();
this.setState({…state,捕获:false,捕获:[…this.state.captures,photoData]})
};
handleLongCapture=async()=>{
setTimeout(()=>this.state.capturing&&this.camera.stopRecording(),20*1000);
const videoData=wait this.camera.recordAsync();
this.setState({…state,capturing:false,captures:[…this.state.captures,videoData]});
}
回来
}
第三种也是更可取的方法是让每个值都处于其“自己的状态”,如的答案所示

function FunctionName() {
    // set the state with it's default values
    const [state, setState] = useState({
        captures: [],
        flashMode: Camera.Constants.FlashMode.off,
        capturing: null,
        cameraType: Camera.Constants.Type.back,
    });

        // we spread the previous state, and then set the value we want to change.
        setFlashMode = (flashMode) => this.setState({...state, flashMode});
        setCameraType = (cameraType) => this.setState({...state, cameraType});
        handleCaptureIn = () => this.setState({...state, capturing: true});
    
        handleCaptureOut = () => {
            if (this.state.capturing)
                this.camera.stopRecording();
        };
    
        handleShortCapture = async () => {
            const photoData = await this.camera.takePictureAsync();
            this.setState({...state, capturing: false, captures: [...this.state.captures, photoData]})
            
        };
        
        handleLongCapture = async () => {
            setTimeout(() => this.state.capturing && this.camera.stopRecording(), 20*1000);
            const videoData = await this.camera.recordAsync();
            this.setState({...state, capturing: false, captures: [...this.state.captures, videoData]});
        }

    return <></>;
}