【问题标题】:【ReactNative】How to execute display: "none" before rendering a screen【ReactNative】如何执行显示:渲染屏幕前的“none”
【发布时间】:2021-08-06 06:02:54
【问题描述】:

我想通过一些代码更改在渲染屏幕之前执行 display: "none"。
现在{goodbotton} 会在屏幕渲染时临时出现。
目前我的代码如下。

...
const [ goodbotton, setGoodBotton ] = useState(<Image source={require('../../images/good_botton_inactive.png')} style={ {width: 65, height: 65} }/>);

 <TouchableOpacity
      onPress={() => {
        if (image.is_my_good) {
          postGood(image.id, false)
        } else {
          setGoodBotton(<Image source={require('../../images/good_botton.png')} style={ {width: 65, height: 65}}/>)
          postGood(image.id, true)
          setTimeout(() => {
            setGoodBotton(<Image source={require('../../images/good_botton_inactive.png')} style={ {width: 65, height: 65}}/>)
          }, 500)
        }
        }}>
        <View style={[image?.is_my_good ? styles.hide_good_botton : {}, commonStyles.shadow]} >
          {goodbotton}
        </View>
      </TouchableOpacity>

const styles = StyleSheet.create({
hide_good_botton: {
    display: "none"
  }
});

你有什么想法吗?

【问题讨论】:

    标签: react-native react-hooks


    【解决方案1】:

    我在项目中看到的最常见的方法是使用状态变量来控制应用程序何时获取或处理某些内容。类似于:

        [isLoading, setIsLoading] = useState(true);
    
        // enter code here
    
        if (isLoading) { return (<View / Spinner / LoadingIcon / PlaceHolderView />) }
    
        return (
            <TouchableOpacity
                onPress={() => {
                 ....
    
    

    【讨论】:

    • 感谢您的评论!我试图适应我的代码。但它没有用。它如何在我的代码中工作?对不起,我是 react 和 javascript 的初学者...
    猜你喜欢
    • 1970-01-01
    • 2017-04-20
    • 2018-03-11
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多