【问题标题】:how to use nested routing with react-native如何在 react-native 中使用嵌套路由
【发布时间】:2016-07-09 11:41:15
【问题描述】:

我将 react-native-router-flux 用于我的应用程序主导航。我需要设置一个嵌套路由器。我有一个地图组件,它有一个带有区域列表的侧栏。当我单击一行时,我需要切换到一个包含属于该区域的细分列表的视图。我查看了一些示例并试图弄清楚。目前控制台中没有错误,但侧边栏中没有显示任何内容。如果有更好的方法,请告诉我。谢谢!

地图路由器

    export default class RootRouter extends Component {
    render() {
        return(
            <Router hideNavBar={true}>
                <Schema name="default" sceneConfig={Navigator.SceneConfigs.FloatFromRight}/>
                <Route name="mapSurveyTerritories" wrapRouter={false} component={MapSurveyTerritories} initial={true}/>
                <Route name="mapSubdivisions" wrapRouter={false} component={MapSubdivisions} />
            </Router>
        );
    }
}

Map Component

BackAndroid.addEventListener('hardwareBackPress', function() {
    Actions.pop();
    return true;
});
export default class Map extends Component {
    constructor(props) {
        super(props);
        this.state = {
            region: Albuquerque,
        };
    }


    render() {
        const { region, markers,surveyTerritories,selectedMarket } = this.state;
        return (
          <View style={styles.container}>
                    <Navbar
                        title="Map"/>

            <View style={styles.innerContainer}>
                <MapView
                    style={styles.map}
                    initialRegion={region}
                    onLongPress={this.onPress.bind(this)}>
                    {markers.map(marker => (
                        <MapView.Marker
                            ref="m1"
                            key={marker.id}
                            coordinate={marker.coordinate}
                            title={marker.name}>
                        </MapView.Marker>
                    ))}
                </MapView>
                <ScrollView style={styles.sidebarContainer}>

                    <MapRouter />
                </ScrollView>
            </View>
          </View>
        );
    }
};

module.exports = Map;

领土

class MapSurveyTerritories extends Component {
    constructor(props) {
        super(props);

        var ds = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 != r2
        });

        this.state = {
            dataSource: ds,
            showProgress: true
        };
    }

    componentDidMount() {
        this.fetchTerritories();
    }

    fetchTerritories() {
        this.setState({
            dataSource: this.state.dataSource
                .cloneWithRows(territoriesAlbuquerque),
            showSpinner: false
        });
    }

    renderRow(rowData) {
        return (
            <TouchableHighlight
                onPress={()=>Actions.mapSubdivisions({selectedTerritory:rowData})}
                underlayColor='#ddd'>
                <View style={styles.row}>
                    <View style={{paddingLeft: 20}}>
                        <Text>
                            <Text style={{ fontWeight: '600' }}> {rowData.properties.name} </Text>
                        </Text>
                    </View>
                </View>
            </TouchableHighlight>
        );
    }

    render() {
        return (
            <View style={{flex: 1,justifyContent: 'flex-start'}}>
<ListView
                    dataSource={this.state.dataSource}
                    renderRow={this.renderRow.bind(this)}/>
            </View>
        );
    }
}

module.exports = MapSurveyTerritories;

细分

class MapSubdivisions extends Component {
    constructor(props) {
        super(props);
var ds = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 != r2
        });
this.state = {
            dataSource: ds
        };
    }

    componentDidMount() {
        this.fetchSubdivisions();
    }

    fetchSubdivisions() {
        console.log('fetchSubdivisions', this.props.selectedTerritory);
        this.setState({
            dataSource: this.state.dataSource
                .cloneWithRows(subdivisionsAlbuquerque),
            showSpinner: false
        });
    }

    renderRow(rowData) {
        return (
            <TouchableHighlight
                onPress={()=>Actions.mapSubdivisionDetail({selectedSubdivision:rowData})}
                underlayColor='#ddd'>
                <View style={styles.row}>
                    <View style={{paddingLeft: 20}}>
                        <Text>
                            <Text style={{ fontWeight: '600' }}> {rowData.properties.name} </Text>
                        </Text>
                    </View>
                </View>
            </TouchableHighlight>
        );
    }

    render() {
         return (
            <View style={{flex: 1,justifyContent: 'flex-start'}}>
                <ListView
                    dataSource={this.state.dataSource}
                    renderRow={this.renderRow.bind(this)}/>
            </View>
        );
    }
}

module.exports = MapSubdivisions;

【问题讨论】:

    标签: reactjs react-native reactjs-flux


    【解决方案1】:

    没错。

    解决这个问题的最好方法是通过组件的 props 使用 Navigator - 因此,在这种情况下,如果您有一个 ListView 组件,您想将其移动到DetailedView 组件,只需调用 navigator.push (应该通过props传递给ListView)。

    换句话说,您将 Navigator 传递给 ListView,在 this.props.navigator 上可用。然后(在 onPress 中),只需在导航器上调用 push,输入您想要渲染的下一个组件(以及您希望它拥有的任何其他道具)。

    在这种情况下,我希望您的代码看起来像这样:

    // Within the ListView component's 'onPress' event
    
    this.props.navigator.push({
      component: DetailedView,
      location: this.props.location,
      // any other props you need to access inside DetailedView
    });
    

    希望这会有所帮助!

    【讨论】:

    • 今晚试试。所以我在sthis Map Component中。我将替换 Navigator 元素并放回原处,。因为这确实会正确加载列表。然后我会在 MainListView qnd DetailView 之间实现导航道具?我来自有角度的背景,所以我习惯于使用 ui-router 进行嵌套视图。我的问题是 DetailView 如何知道在哪里呈现自己?
    • 不用担心。是的,您需要将导航器作为道具通过 MainListView 传递给 DetailView。
    • 我用 MainListView 更新了帖子。你介意告诉我我需要做什么吗?我明白你在说什么,就它需要如何工作而言。就是不知道怎么实现
    • 抱歉 - 所以从最后一条评论中删除了我的编辑。我的意思是写:不用担心。是的,您需要将导航器作为道具通过 MainListView 传递给 DetailView。您可以将导航器视为一个堆栈,从其给定的任何内容中推送和弹出视图。如果你给 Navigator 对象一个属性“SceneConfigs”,你可以将它设置为“FloatFromBottom”(或左/右/等) - 所以它知道从哪里进来。
    • 好的,我更新了帖子。我不知道如何让它继续下去。请看一看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2022-08-17
    • 2018-06-12
    • 2021-09-26
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多