【问题标题】:How to navigate page with React Native如何使用 React Native 导航页面
【发布时间】:2017-02-12 21:15:53
【问题描述】:

我有一个用于列出项目的组件,我想添加可以转到不同页面的功能,并且该页面包含有关该项目的详细信息。目前,这是我列出项目的代码。

import React, { Component } from 'react';
import { ScrollView } from 'react-native';
import axios from 'axios';
import CarDetail from './CarDetail';

const API_URL = 'http://localhost:3000';

class CarList extends Component {
  state = { cars: [] };

  componentWillMount() {
    console.log('Mount');
    axios.get(`${API_URL}/cars`)
    .then(response => this.setState({ cars: response.data.cars }));
  }

  renderCars() {
    return this.state.cars.map(car => <CarDetail key={car.id} car={car} />
    );
  }

  render() {
    console.log(this.state.cars);
    return (
      <ScrollView>
        {this.renderCars()}
      </ScrollView>
    );
  }
}

export default CarList; 

这是描述物品的代码

import React from 'react';
import { Text, View, Image } from 'react-native';
import { Actions } from 'react-native-router-flux';
import Card from '../material/Card';
import CardSection from '../material/CardSection';


const CarDetail = ({ car }) => {
  const imageURI = 'https://yt3.ggpht.com/-HwO-2lhD4Co/AAAAAAAAAAI/AAAAAAAAAAA/p9WjzQD2-hU/s900-c-k-no-mo-rj-c0xffffff/photo.jpg';
  const { make, model } = car;
  function showCarDetail() {
    Actions.showCar();
  }
    return (
      <Card>
        <CardSection>
        <View style={styles.containerStyle}>
        <Image
        style={styles.imageStyle}
        source={{ uri: imageURI }}
        />
        </View>
        <View style={styles.headContentStyle}>
        <Text
        style={styles.headerTextStyle}
        onPress={showCarDetail()}
        >
        {make}
        </Text>
        <Text>{model}</Text>
        </View>
        </CardSection>
        <CardSection>
          <Image
          style={styles.picStyle}
          source={require('./car.jpg')}
          />
        </CardSection>
      </Card>
    );
};

const styles = {
  headContentStyle: {
    flexDirection: 'column',
    justifyContent: 'space-around'
  },
  headerTextStyle: {
    fontSize: 18
  },
  imageStyle: {
    height: 50,
    width: 50
  },

  containerStyle: {
    justifyContent: 'center',
    alignItems: 'center',
    marginLeft: 10,
    marginRight: 10
  },

  picStyle: {
    height: 300,
    flex: 1,
    width: null
  }
};

export default CarDetail;

我该如何更改我的代码?谁能举个例子?

【问题讨论】:

    标签: javascript reactjs react-native react-redux


    【解决方案1】:

    您必须使用某种导航组件。那里有很多,但我个人使用的是内置在 React Native 中的。 https://facebook.github.io/react-native/docs/navigator.html

    【讨论】:

    • 是的,我以前做过。但是当我显示列表时,页面会自动定向到那个详细页面,这不是我想要的。我还是希望列表页能像以前一样显示,详情页只有点击文字后才会显示
    猜你喜欢
    • 2021-04-27
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多