代码之家  ›  专栏  ›  技术社区  ›  Mayuresh Patil

无法在react native中导航到其他屏幕

  •  0
  • Mayuresh Patil  · 技术社区  · 7 年前

    我在代码中使用道具来显示项目,但单击后我无法导航到任何其他屏幕。我也被定义为 const { navigate } = this.props.navigation; 但在屏幕截图中显示错误。

    这是我的密码。此代码与 Indprotype.js 文件(不使用两个单独的.js文件)

      class Withwish extends React.Component {
    
          ProPressed(productid){
            const { navigate } = this.props.navigation;
            const params = { productid };
            navigate('Product', params);
          }
    
        render() {
              // const { navigate } = this.props.navigation;
          return (
                      <View style={styles.word}>
                      <TouchableHighlight onPress={() => this.ProPressed(this.props.id)}>
                        <Image source={{uri: 'https://res.cloudinary.com/hu2lcbj2n/' + this.props.image}} />
                      </TouchableHighlight>  
                  </View>
          );
        }
      }
    
    
      export default class Indprotype extends React.Component {
         // const { navigate } = this.props.navigation;
        render() {
           const items = this.state.qwerty.data;
          return (
              <GridView
                  renderItem={item => (
                    <Withwish
                      image = {item.p1.image}
                      id = {item.p1.id}
                     />
                )}
              />
          );
        }
      }
    

    error

    1 回复  |  直到 7 年前
        1
  •  1
  •   Rob Walker    7 年前

    试试这个。

    基本上我通过了 navigation 来自的道具 Indprotype 组件到 Withwish 组成部分

    注:我在这里假设 Indprotype型 自身接收 航行 路由器或类似设备的道具。

    class Withwish extends React.Component {
    
        ProPressed(productid){
          const { navigate } = this.props.navigation;
          const params = { productid };
          navigate('Product', params);
        }
    
      render() {
            // const { navigate } = this.props.navigation;
        return (
                    <View style={styles.word}>
                    <TouchableHighlight onPress={() => this.ProPressed(this.props.id)}>
                      <Image source={{uri: 'https://res.cloudinary.com/hu2lcbj2n/' + this.props.image}} />
                    </TouchableHighlight>  
                </View>
        );
      }
    }
    
    
    export default class Indprotype extends React.Component {
        // const { navigate } = this.props.navigation;
      render() {
          const items = this.state.qwerty.data;
        return (
            <GridView
                renderItem={item => (
                  <Withwish
                    navigation = {this.props.navigation}
                    image = {item.p1.image}
                    id = {item.p1.id}
                    />
              )}
            />
        );
      }
    }