Javascript 如何在React Native中的获取url中插入参数?
我在React Native方面的技能是基本的,我想在url中插入参数id以根据类别显示帖子Javascript 如何在React Native中的获取url中插入参数?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我在React Native方面的技能是基本的,我想在url中插入参数id以根据类别显示帖子 export default class PostByCategory extends Component { static navigationOptions = ({ navigation }) => ({ title: `${navigation.state.params.Title}`, }); constructor(props) { super(pr
export default class PostByCategory extends Component {
static navigationOptions = ({ navigation }) => ({
title: `${navigation.state.params.Title}`,
});
constructor(props) {
super(props);
this.state = {
isLoading: true,
};
}
componentDidMount() {
return fetch(ConfigApp.URL+'json/data_posts.php?category='`${navigation.state.params.IdCategory}`)
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson
}, function() {
});
})
.catch((error) => {
console.error(error);
});
}
您必须将
navigation.state.params.IdCategory
替换为this.props.navigation.state.params.IdCategory
手动将参数连接到url不是一个好的做法。我建议您查看以了解如何正确构造查询字符串。上面的代码有什么问题?我通过将`${navigation.state.params.IdCategory}替换为+this.props.navigation.state.params.IdCategory}来解决它
componentDidMount() {
return fetch(ConfigApp.URL+'json/data_posts.php?category='+this.props.navigation.state.params.IdCategory)
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson
}, function() {
});
})
.catch((error) => {
console.error(error);
});
}