简体   繁体   English

我可以将 API 中的信息放到另一个页面吗?

[英]Can I put information from an API to another page?

The title is the best I can really phrase it, however, heres my Microsoft Paint version of what I'm trying to do.标题是我能真正表达出来的最好的标题,但是,这是我正在尝试做的我的 Microsoft Paint 版本。 If you guys know how to do it feel free to reply.如果你们知道怎么做,请随时回复。

Heres what it is doing这是它在做什么

它现在在做什么

Heres what I want it to do这是我想要它做的事情

我想让它做什么

Sorry for the bad drawings not the most artistic.对不起,糟糕的图画不是最艺术的。

React basically is a single page application so one way in doing what you want is to use react-router-dom when the submit button is clicked, you send the form data to the backend and then dispatch a new route that will display the component that has the API data. React 基本上是一个单页应用程序,因此一种方法是在单击提交按钮时使用react-router-dom ,将表单数据发送到后端,然后调度一个新路由来显示组件有API数据。 inside the componentDidMount life cycle of the component that will display the api data you make a fetch to get the data.在组件的componentDidMount生命周期内,将显示 api 数据,您通过获取数据来获取数据。

I'm not sure whats the use case you are trying to solve, but I will assume that it is something similar to how search could work.我不确定您要解决的用例是什么,但我会假设它类似于搜索的工作方式。 ie: You enter something in input fields, push submit button -> hide form you are currently in and show results in next page.即:您在输入字段中输入内容,按下提交按钮-> 隐藏您当前所在的表单并在下一页显示结果。

Pseudo code could look like this伪代码可能看起来像这样

class Search extends React.Component {
  state = {
    query: "",
    results: null
  }

  handleOnChange = e => {
    this.setState({query: e.target.value})
  }

  formSubmit = () => {
    //do your request to api
       .then(response => this.setState({results: response}))
  }

  render() {
    return(
      this.state.results === null ? (
        <form>
           <input type="text" value={this.state.query} onChange={this.handleOnChange}/>
           <button type="submit" onClick={this.formSubmit}>Submit</button>
        </form>
      ):(
        <div>{this.state.results}</div>
      )
    ) 
  }
}

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

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