第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

文本框值未傳遞給 api 調(diào)用

文本框值未傳遞給 api 調(diào)用

縹緲止盈 2021-11-04 16:30:01
我按照以下鏈接嘗試做一個 poc https://www.reddit.com/r/reactjs/comments/82mxz3/how_to_make_an_api_call_on_props_change在 Searchbar 中,我創(chuàng)建了一個文本框,當(dāng)我提供 componentWillRecieveProps 并打印值以將其傳遞給 api 但沒有打印時,我正在獲取值但在 index.js 中。你能告訴我如何修復(fù)它嗎?在下面提供更新的代碼沙箱和代碼片段。https://codesandbox.io/s/eloquent-galielo-14874//import React from "react";import ReactDOM from "react-dom";import React, { Fragment, useState, Component } from "react";import "./styles.css";class SearchBar extends Component {  state = {    groupCheckBoxValues: [],    groupRadioValue: "PRO"  };  getInitialState() {    return { value: "Hello!" };  }  handleChange(event) {    console.log("handleChange", event.target.value);    this.setState({ value: event.target.value });  }  componentWillReceiveProps({ search }) {    console.log(search);  }  componentDidMount() {    this.fetchdata("story");  }  fetchdata(type = "", search_tag = "") {    var url = "https://hn.algolia.com/api/v1/search?tags=";    fetch(`${url}${type}&query=${search_tag}`)      .then(res => res.json())      .then(data => {        this.props.getData(data.hits);      });  }  render() {    return (      <input        type="text"        value={this.state.value}        onChange={this.handleChange}      />    );  }}export default SearchBar;
查看完整描述

1 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗 獲得超8個贊

您實(shí)際上并未將文本輸入的值傳遞給提取請求。


我推薦這樣的東西:


class SearchBar extends Component {


  searchByKeyword = ({target}) => {

    await this.getQuery("story", target.value)

  }


  async componentDidMount() {

    await this.getQuery("story", "butts");

  }


  getQuery = async(type = "", search_tag = "") => {

    var url = "https://hn.algolia.com/api/v1/search?tags=";

    const resp = await fetch(`${url}${type}&query=${search_tag}`)

    return resp.json()

  }


  render() {

    return (

      <input

        type="text"

        onChange={this.searchByKeyword}

      />

    );

  }

}

我刪除了狀態(tài)和事物,因為它似乎與問題并不完全相關(guān)。


查看完整回答
反對 回復(fù) 2021-11-04
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號