Hi everyone, I am new to react. any help is deeply appreciated!
I am using React with TypeScript but it doesn’t have a different behaviour as far as I know, in my case.
My goal is to call api based on state change, Every time state changes, I want the api call triggered.
MyComponent
import * as React from 'react';
export class Verifier extends React.Component<{}, IState> {
constructor(props: {}) {
super(props);
this.state = {
str: '',
response: []
}
}
verifyIt(myArg) {
var apiCall = fetch(myApi + 'myArg');
apiCall.then(response => {
return response.json();
}).then(result => {
console.log(result);
this.setState({
response: result
})
});
}
componentWillMount() {
let myArg = this.state.myArg;
this.verifyIt(myArg);
}
handleSubmit(e: any) {
e.preventDefault();
this.setState({
myArg: this.state.str
})
}
render() {
return (
<div>
<form onSubmit={(e) => this.handleSubmit(e)}>
<input
type='text'
placeholder='someone@example.com'
value={ this.state.str}
onChange={(e) => this.setState({ str: e.target.value })} />
<button type='submit'>Check</button>
</form>
{/* <section>{ Render messages here }</section> */}
</div>
)
}
}
interface IState {
myArg: string;
response: Array<string>;
}
Please let me know if you need any more details. Thanks in advance.