gpt4 book ai didi

reactjs - 如何将嵌套路由与react-native结合使用

转载 作者:行者123 更新时间:2023-12-03 13:24:31 26 4
gpt4 key购买 nike

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

map 路由器

    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;

最佳答案

对。

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

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

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

// 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
});

希望这有帮助!

关于reactjs - 如何将嵌套路由与react-native结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36156876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com