简体   繁体   English

如何在 React Native 中 3 秒后隐藏顶部栏

[英]How to hide top bar after 3 seconds in React Native

I have been trying to achieve hiding (or slide up) my react native startup app top bar after 3 seconds, then have a button to press to show the top bar, but could not.我一直试图在 3 秒后隐藏(或向上滑动)我的反应原生启动应用程序顶部栏,然后按下按钮以显示顶部栏,但不能。 I've searched online how to do it, but have not seen good solution for it.我在网上搜索了如何做到这一点,但没有看到好的解决方案。 Please I need help here, the below is snippet code of what I tried doing but it did not work.请我在这里需要帮助,下面是我尝试做的代码片段,但它不起作用。

<HeaderHomeComponent /> is the top header I created and imported it in my Home screen <HeaderHomeComponent />是我在主屏幕中创建并导入的顶部 header

 const Home = () => { const camera = useRef(); const [isRecording, setIsRecording] = useState(false); const [flashMode, setFlashMode] = useState(RNCamera.Constants.FlashMode.off); const [cameraType, setCameraType] = useState(RNCamera.Constants.Type.front); const [showHeader, setShowHeader] = useState(true); const onRecord = async () => { if (isRecording) { camera.current.stopRecording(); } else { setTimeout(() => setIsRecording && camera.current.stopRecording(), 23*1000); const data = await camera.current.recordAsync(); } }; setTimeout(()=> setShowHeader(false),3000); const DisplayHeader = () => { if(showHeader == true) { return <HeaderHomeComponent /> } else { return null; } } // useEffect(() => { // DisplayHeader(); // }, []) return ( <View style={styles.container}> <RNCamera ref={camera} type={cameraType} flashMode={flashMode} onRecordingStart={() => setIsRecording(true)} onRecordingEnd={() => setIsRecording(false)} style={styles.preview} /> <TouchableOpacity style={styles.showHeaderButton} onPress={() => { setShowHeader(;showHeader); }}> <Button title='Show' /> </TouchableOpacity> <HeaderHomeComponent />

This should help you get started in the right direction, you can use animation based on your preference to this code.这应该可以帮助您朝着正确的方向开始,您可以根据您对该代码的偏好使用 animation。

import React, {useState, useEffect} from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import Constants from 'expo-constants';



export default function App() 
{


  const [showStatusbar, setShowStatusbar] = useState(true)

  useEffect(() => 
  {
    setTimeout(() => 
    {
      setShowStatusbar(false)
    }, 3000)

  }, [])

  return (
    <View style={styles.container}>
        
        {
          showStatusbar 
          ? <View style = {styles.statusBar}>
              <Text>Status Bar</Text>
            </View>
          : null
        }
        

        <Button title = "Show Status bar" onPress = {() => setShowStatusbar(true)}/>
     
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ecf0f1',
  },
  statusBar:
  {
    height: 50,
    backgroundColor:'lightblue',
    justifyContent:'center',
    alignItems:'center'

  }
  
});

You were really close.你真的很亲近。

This is how it should be done:应该这样做:

useEffect(() => {
    setTimeout(toggleHeader,3000);
}, []);

const toggleHeader = () => setShowHeader(prev => !prev);

Then inside the "return":然后在“返回”内部:

 {showHeader && <HeaderHomeComponent />}

As simple as that.就如此容易。

Instead of setTimeout use Animation.代替 setTimeout 使用 Animation。 or Check this lib: https://www.npmjs.com/package/react-native-animated-hide-view or check below answers which might help you Hide and Show View With Animation in React Native v0.46.或检查此库: https://www.npmjs.com/package/react-native-animated-hide-view或检查以下答案,这可能有助于您在 React Native v0.46 中使用 Animation 隐藏和显示视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM