【问题标题】:How to concatenate url in React Native如何在 React Native 中连接 url
【发布时间】:2019-09-25 09:15:13
【问题描述】:
"questionnaire": [
        {
            "id": 6,
            "status": "1",
            "name": "Driniking water 24h",
            "lang_id": 2,
            "zona_id": 7,
            "created_at": "2019-05-07 10:35:18",
            "updated_at": "2019-06-01 10:01:08",
            "questionnaire_category": 1,
            "question_number": 1,
            "content": "Driniking water  Driniking water  Driniking water  Driniking water"
        }
    ],
 "next": {
        "id": 7,
        "status": "1",
        "name": "Public Lighting",
        "lang_id": 1,
        "zona_id": 7,
        "created_at": "2019-05-22 05:04:27",
        "updated_at": "2019-06-01 10:00:37",
        "questionnaire_category": 1,
        "question_number": 2,
        "content": "Public Lighting Public Lighting Public Lighting Public Lighting"
    }

我必须将问卷的id和next的id连接起来,这是我的urlhttps://quiet-cove-41253.herokuapp.com/api/questionApi/1,我不知道如何连接这两个id??????

var response = await fetch(`https://quiet-cove-41253.herokuapp.com/api/questionApi/1/6`+this.state.nextData.id);

state = {
    data: [],
    nextData: []
  };

fetchData = async () => {
    var response = await fetch(`https://quiet-cove-41253.herokuapp.com/api/questionApi/1/6`+this.state.nextData.id);
    var json = await response.json();
    this.setState({ data: json.questionnaire, nextData: json.next });
  };

  componentDidMount() {
    this.fetchData();
  }

render(){
return(
<View><Text>{this.state.data.id}</Text>
                <Text>{this.state.data.name}</Text>
                <Text>{this.state.data.content}</Text>
</View>
)
}

我的意思是,从 下一个 id 我的问题将会改变

【问题讨论】:

  • 你试过这样https://quiet-cove-41253.herokuapp.com/api/questionApi/1/6${this.state.nextData.id}
  • 只显示 nextData id,我可以在 url 中添加 questionnaire id

标签: javascript json react-native multidimensional-array


【解决方案1】:

你可以试试

var response = await fetch(`https://quiet-cove-41253.herokuapp.com/api/questionApi/1/6,${this.state.nextData.id}`);

【讨论】:

  • 现在我可以得到它们,但问题没有改变
  • 您还需要动态传递 question_id,因为正在传递 6,它将始终为 6
  • 但我不知道怎么做?
  • 您也应该将 question_id 存储在 state 中并传递 ${this.state.question_id} 而不是 6
猜你喜欢
  • 2015-07-10
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多