reactjs - react 原生切换组件没有状态

标签 reactjs react-native

我只想切换 SearchButton 和 SearchIcon ,我使用以下代码

  searchBarButton() {
    Actions.refresh();
    this.setState({ showSearchBar: !this.state.showSearchBar });
  }

 render() {
    if (this.state.showSearchBar) {
      return (
        <Header>
          <View style={styles.searchHolder}>
            <Item style={styles.searchBar}>
              <Icon name="ios-search" />
              <Input placeholder="Search" />
            </Item>
            <Button style={styles.searchButton} onPress={this.searchBarButton}>
              <Text>Search</Text>
            </Button>
          </View>
        </Header>
      );
    }
    return (
      <Header>
            <Button onPress={() => this.searchBarButton()} transparent>
              <Icon name="search" style={styles.bigblue} />
            </Button>
      </Header>
    );
  }

所以基本上最初速度非常快,但是当我的场景包含平面列表中的大量项目时,切换之间大约有 1 到 2 秒的延迟。我猜这是由于重新渲染页面中的所有项目所致。

那么如何以更简单、更高效的方式切换此功能,而无需在不使用状态的情况下重新渲染整个页面

最佳答案

我认为你必须使用 style 属性隐藏其中一个。

先渲染两者,然后将其中一个切换为隐藏(通过使用状态),

<Header>
  <View style={[this.state.showSearchBar && styles.hidden]}>Button</View>
  <View style=[{!this.state.showSearchBar && styles.hidden}]>Icon</View>
</Header>

不要将它们完全从虚拟dom中删除(我不知道在移动设备中如何调用它)

关于reactjs - react 原生切换组件没有状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623034/

相关文章:

javascript - 购买成功后 react 推送查看

javascript - 模块化和抽象 react 组件功能

reactjs - 在 react native + typescript 应用程序上运行 jest 时出错(Jest 遇到意外 token )

react-native - react native /博览会 : fetch throws "Network request failed" error on 401 response with formData content type

reactjs - 从 webview 重定向到 React Native 组件

react-native - 为什么不生成设备 token ?

node.js - 登录成功后如何发送JWT到前端服务器存储到localStorage?

javascript - <fieldset> 以 React 形式破坏 onChange

javascript - 无法在 React.js 中使用 useState 进行渲染

android - react-native 如何在特定时间安排后台作业