简体   繁体   English

如何在React Native的获取URL中插入参数?

[英]How to insert params in the fetch url in React Native?

My skills in React Native is basic, i want to insert the params id in the url to show the posts according to the category. 我在React Native中的技能是基本技能,我想在URL中插入params id以根据类别显示帖子。

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

You have to replace navigation.state.params.IdCategory with this.props.navigation.state.params.IdCategory . 您必须用this.props.navigation.state.params.IdCategory替换navigation.state.params.IdCategory

It's not a good practice to manually concat your params to the url. 手动将参数关联到url不是一个好习惯。 I suggest you look at this question to learn how to properly construct your query string. 我建议您看一下这个问题,以学习如何正确构造查询字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM