【问题标题】:React Conditional Render and NavbarReact 条件渲染和导航栏
【发布时间】:2018-09-19 17:02:33
【问题描述】:

我正在通过主渲染函数中的状态和 switch 语句控制应在我的应用程序屏幕上显示的组件。我正在用 react-native 写这个,但这是一个反应结构问题。

我还有一个 Navbar 组件,理想情况下,我希望仅在用户单击 Navbar 本身中的链接时重新呈现,但我不知道如何使用 switch 语句设置来做到这一点的好方法现在,似乎我每次都必须根据状态满足的条件重新渲染导航栏。

我的问题是,有没有办法我仍然可以在渲染方法中使用条件渲染组件,就像我在下面一样,并且有一个组件总是像导航栏一样呈现在屏幕顶部?我知道这对于 React Router 之类的东西是可能的,但是有没有更好的方法来构建它而不使用像 React Router 这样的工具或每次都重新渲染 NavBar 组件?

import React from 'react';

import GPS from './GPS/gps';
import Home from './Home';
import Photo from './Camera/Photo';

export default class App extends React.Component {
  constructor() {
    super();

    this.state = {
      hasCameraPermission: null,
      type: Camera.Constants.Type.back,
      currentView: null,
      currentImage: null,
      navigation: 'Overview'
    };

    this.updateNavigation = this.updateNavigation.bind(this);
  }

  updateNavigation(view) { //Update view function
    this.setState({currentView: view});
  }


  render() {
    const { currentView } = this.state;

    switch(currentView) {
      case null:
      return (
        <Home updateNav={this.updateNavigation} />
        );
        break;

      case 'GPS':
      return (
        <View>
          <GPS />
          <Text onPress={() => this.setState({currentView: null})}>Back</Text>
        </View>
      );
        break;

      case 'Camera':
      return (
          <Photo updateNav={this.updateNavigation} />
       );
       break;

       case 'viewPicture':
       return (
        <View>
          <Image source={{uri: this.state.currentImage.uri}} style={{width: this.state.currentImage.width/10, height: this.state.currentImage.height/12}} />
        </View>
       );
       break;

    }
  }
}

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    始终保持渲染尽可能干净。

    您可以使用 && 运算符来做同样的事情,而不是使用 switch case。使用 && 运算符并检查每种情况并相应地呈现。检查下面的代码以获得更好的理解。

    render() {
        const { currentView } = this.state;
        return(
          {currentView == null && (
            <Home updateNav={this.updateNavigation} />
            )}
          {currentView == "GPS" && (
            <View>
              <GPS />
              <Text onPress={() => this.setState({currentView: null})}>Back</Text>
            </View>
            )}
    
          {currentView == "Camera" && (
            <View>
              <Photo updateNav={this.updateNavigation} />
            </View>
            )}
    
          {currentView == "viewPicture" && (
            <View>
              <Image source={{uri: this.state.currentImage.uri}} style={{width: this.state.currentImage.width/10, height: this.state.currentImage.height/12}} />
            </View>
            )}
        )
    
      }
    

    【讨论】:

    • 感谢您的回复!是否有一种方法可以始终在渲染方法的开头渲染类似 Navbar 组件的内容,但不必为每个组件使用 && 运算符显式地将其置于条件中?
    • 是的,您可以在没有任何条件的情况下返回导航栏组件,以便始终呈现。我对当前 View 值不利,我添加了 GPS 组件而不是 Home。我现在编辑了。
    猜你喜欢
    • 2022-11-23
    • 2019-09-10
    • 2020-09-09
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2019-10-02
    相关资源
    最近更新 更多