简体   繁体   English

我在查找有关将我的状态传递到另一个文档的组件的文档/视频时遇到问题

[英]I'm having trouble finding documentation/video about passing my state to another document's component

i've got my searchbar on the container, setting 'search' state ontextchange.. and i want to pass that state to the nested component(in a separate document) so it can filter the images displayed based on the search state. 我在容器上设置了搜索栏,将onsearchchange状态设置为ontextchange ..,我想将该状态传递给嵌套组件(在单独的文档中),以便它可以根据搜索状态过滤显示的图像。

i've tried to pass search everywhere and import/export search. 我试图通过搜索到处和导入/导出搜索。 i've also googled this endlessly, and cannot find anything pertaining to the issue. 我也无休止地在Google上搜索,找不到与该问题有关的任何内容。 maybe i don't have the terminology right. 也许我没有正确的术语。

Components folder has the AppTabNavigator folder which contains this file titled HomeTab.js I am trying to pass state from HomeTab.js to CardComponent.js pasted below. Components文件夹具有AppTabNavigator文件夹,其中包含名为HomeTab.js的文件,我正尝试将状态从HomeTab.js传递到下面粘贴的CardComponent.js。

import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import { Container, Content, Icon } from 'native-base';
import CardComponent from '../CardComponent';
import { SearchBar } from 'react-native-elements';

class HomeTab extends Component {
  static navigationOptions = {
    tabBarIcon: ({ tintColor }) => (
      <Icon name="ios-search" style={{ color: tintColor }} />
    )
  };

  state = {
    search: ''
  };
  updateSearch = search => {
    this.setState({ search });
    console.warn(search);
  };

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

    return (
      <Container style={styles.container}>
        <SearchBar
          containerStyle={{ backgroundColor: 'transparent' }}
          placeholder="Filter for..."
          placeholderTextColor="white"
          onChangeText={this.updateSearch}
          value={search}
        />
        <Content>
          <CardComponent search={this.state.search} />
        </Content>
      </Container>
    );
  }
}
export default HomeTab;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'black'
  }
});

The Components folder that contains the AppTabNavigator folder, also contains this file titled CardComponent.js ... I am trying to pass state to CardComponent.js from HomeTab.js 包含AppTabNavigator文件夹的Components文件夹也包含名为CardComponent.js的文件...我试图将状态从HomeTab.js传递给CardComponent.js

import React, { Component } from 'react';
import { View, Dimensions, Image, Text, TouchableOpacity } from 'react-native';

var images = [
  require('../assets/IMG-0028.jpeg'),
  require('../assets/IMG-0048.jpeg'),
  require('../assets/IMG-0064.jpeg'),
  require('../assets/IMG-0089.jpeg'),
  require('../assets/IMG-0119.jpeg'),
  require('../assets/IMG-0151.jpeg'),
  require('../assets/IMG-0152.jpeg'),
  require('../assets/IMG-0153.jpeg'),
  require('../assets/IMG-0154.jpeg'),
  require('../assets/IMG-0155.jpeg'),
  require('../assets/IMG-0184.jpeg'),
  require('../assets/IMG-0221.jpeg'),
  require('../assets/IMG-0268.jpeg'),
  require('../assets/IMG-0309.jpeg'),
  require('../assets/IMG-0320.jpeg'),
  require('../assets/IMG-0474.jpeg'),
  require('../assets/IMG-0707.jpeg'),
  require('../assets/IMG-0860.jpeg')
];
var { width, height } = Dimensions.get('window');

class CardComponent extends Component {
  renderHome = () => {
         console.log(search);
    return images.map((image, index) => {
      return (
        <TouchableOpacity onPress={() => console.warn(index)} key={index}>
          <View
            key={index}
            style={[
              { width: width / 3 },
              { height: height / 3 },
              { marginBottom: 2 },
              index % 3 !== 0 ? { paddingLeft: 2 } : { paddingLeft: 0 }
            ]}
          >
            <Image
              style={{ flex: 1, width: undefined, height: undefined }}
              source={image}
            />
          </View>
        </TouchableOpacity>
      );
    });
  };
  render() {
    return (
      <View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
        {this.renderHome()}
      </View>
    );
  }
}
export default CardComponent;

Expected to be able to search my images by ID so I could then learn how to tag them within the app and search those tags. 期望能够通过ID搜索我的图像,这样我就可以学习如何在应用程序中对其进行标记并搜索这些标记。 Actual results hae been frustration and lost time. 实际结果令人沮丧和浪费时间。

Updated code of CardComponent: CardComponent的更新代码:

class CardComponent extends Component {
  renderHome = () => {
         console.log(this.props.search);
    return images.map((image, index) => {
      return (
        <TouchableOpacity onPress={() => console.warn(index)} key={index}>
          <View
            key={index}
            style={[
              { width: width / 3 },
              { height: height / 3 },
              { marginBottom: 2 },
              index % 3 !== 0 ? { paddingLeft: 2 } : { paddingLeft: 0 }
            ]}
          >
            <Image
              style={{ flex: 1, width: undefined, height: undefined }}
              source={image}
            />
          </View>
        </TouchableOpacity>
      );
    });
  };
  render() {
    return (
      <View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
        `{this.renderHome.bind(this)()}`
      </View>
    );
  }
}
export default CardComponent;

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

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