-3
Hey, guys, what’s up? I have a question and would like help, I am using React to make a web page where any user can use it to consume Apis. To do this I am using the Tips, my question is the following, the idea of the page is the user use the input to enter the address of the API he wants to consume by clicking send the data is returned to him. However after searching a lot I only found the tutorials with the Axios baseURL with fixed addresses, I do not know how to recover the state of what the user typed and make the request to him, IE, leave the dynamic baseURL. Below is my code and the page, I thank you.
import React, { Component } from 'react';
import axios from 'axios';
export class Home extends Component {
static displayName = Home.name;
constructor() {
super()
this.state = {
url: '',
result: '',
method: 'post',
token: '',
body: '',
};
}
UrlFunction = (eventUrl) => { this.setState({ url: eventUrl.target.value }) }
MethodFunction = (eventMethod) => { this.setState({ method: eventMethod.target.value }) }
TokenFunction = (eventToken) => { this.setState({ token: eventToken.target.value }) }
JsonFunction = (eventBody) => { this.setState({ body: eventBody.target.value }) }
async componentDidMount() {
const response = await (await axios.get(this.state.url)).data
const result = JSON.stringify(response, undefined, 4)
console.log(result)
this.setState({ result })
}
render() {
const { result } = this.state;
return (
<div>
<form onSubmit={this.componentDidMount} >
<select value={this.state.method} onChange={this.MethodFunction}>
<option value="get">GET</option>
<option value="post">POST</option>
<option value="patch">PATCH</option>
<option value="put">PUT</option>
<option value="delete">DELETE</option>
</select>
<label>
URL
<input type="text" name="url" value={this.state.url} onChange={this.UrlFunction} />
</label>
<label>
Token : Bearer
<input type="text" name="token" value={this.state.token} onChange={this.TokenFunction} />
</label>
<button type="submit">Enviar</button>
</form>
<textarea value={this.state.body} onChange={this.JsonFunction} cols="75" rows="20">
</textarea>
<textarea value={result} cols="75" rows="20" readOnly disabled></textarea>
</div>
);
}
}
I don’t get it, because you’re already saving a new state to url? I don’t get it at all
– novic
in the above state I left the url started just to demonstrate it, the idea is to leave it empty for the user to fill in.
– user206751