How to use Redirect in React.js with fetching data from API
up vote
0
down vote
favorite
I am trying to create a login form which validates data by fetching API data. Here I am getting data of Star Wars heroes where username will be their name and password will be their birth year.
My code is:
class LoginPage extends Component{
constructor(props){
super(props);
this.state={
username:'',
password:'',
data:null
}
this.hadleUsername=this.hadleUsername.bind(this);
this.handlePass=this.handlePass.bind(this);
}
hadleUsername(e){
this.setState({username:e.target.value})
}
handlePass(e){
this.setState({password:e.target.value})
}
submit(e){
console.log(this.state.data);
if(this.state.data!=null){
this.state.data.results.map(data=>{
if(data.name===this.state.username && data.birth_year===this.state.password){
return <Redirect to='/info/'/>
}
});
}
}
async componentDidMount(){
const response = await fetch('https://swapi.co/api/people/');
const json = await response.json();
this.setState({data:json});
}
render(){
return(
<form className='login-container'>
<h3>Login Page</h3>
<label>Username:</label>
<input
type='text'
value={this.state.username}
placeholder='username'
onChange={this.hadleUsername}
/>
<br/><br/>
<label>Password:</label>
<input
type='password'
value={this.state.password}
placeholder='password'
onChange={this.handlePass}
/>
<br/><br/>
<button type='submit' onClick={this.submit.bind(this)}>submit</button>
</form>
)
}
}
Is there a better way to do it?
node.js reactjs react-native redirect render
add a comment |
up vote
0
down vote
favorite
I am trying to create a login form which validates data by fetching API data. Here I am getting data of Star Wars heroes where username will be their name and password will be their birth year.
My code is:
class LoginPage extends Component{
constructor(props){
super(props);
this.state={
username:'',
password:'',
data:null
}
this.hadleUsername=this.hadleUsername.bind(this);
this.handlePass=this.handlePass.bind(this);
}
hadleUsername(e){
this.setState({username:e.target.value})
}
handlePass(e){
this.setState({password:e.target.value})
}
submit(e){
console.log(this.state.data);
if(this.state.data!=null){
this.state.data.results.map(data=>{
if(data.name===this.state.username && data.birth_year===this.state.password){
return <Redirect to='/info/'/>
}
});
}
}
async componentDidMount(){
const response = await fetch('https://swapi.co/api/people/');
const json = await response.json();
this.setState({data:json});
}
render(){
return(
<form className='login-container'>
<h3>Login Page</h3>
<label>Username:</label>
<input
type='text'
value={this.state.username}
placeholder='username'
onChange={this.hadleUsername}
/>
<br/><br/>
<label>Password:</label>
<input
type='password'
value={this.state.password}
placeholder='password'
onChange={this.handlePass}
/>
<br/><br/>
<button type='submit' onClick={this.submit.bind(this)}>submit</button>
</form>
)
}
}
Is there a better way to do it?
node.js reactjs react-native redirect render
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to create a login form which validates data by fetching API data. Here I am getting data of Star Wars heroes where username will be their name and password will be their birth year.
My code is:
class LoginPage extends Component{
constructor(props){
super(props);
this.state={
username:'',
password:'',
data:null
}
this.hadleUsername=this.hadleUsername.bind(this);
this.handlePass=this.handlePass.bind(this);
}
hadleUsername(e){
this.setState({username:e.target.value})
}
handlePass(e){
this.setState({password:e.target.value})
}
submit(e){
console.log(this.state.data);
if(this.state.data!=null){
this.state.data.results.map(data=>{
if(data.name===this.state.username && data.birth_year===this.state.password){
return <Redirect to='/info/'/>
}
});
}
}
async componentDidMount(){
const response = await fetch('https://swapi.co/api/people/');
const json = await response.json();
this.setState({data:json});
}
render(){
return(
<form className='login-container'>
<h3>Login Page</h3>
<label>Username:</label>
<input
type='text'
value={this.state.username}
placeholder='username'
onChange={this.hadleUsername}
/>
<br/><br/>
<label>Password:</label>
<input
type='password'
value={this.state.password}
placeholder='password'
onChange={this.handlePass}
/>
<br/><br/>
<button type='submit' onClick={this.submit.bind(this)}>submit</button>
</form>
)
}
}
Is there a better way to do it?
node.js reactjs react-native redirect render
I am trying to create a login form which validates data by fetching API data. Here I am getting data of Star Wars heroes where username will be their name and password will be their birth year.
My code is:
class LoginPage extends Component{
constructor(props){
super(props);
this.state={
username:'',
password:'',
data:null
}
this.hadleUsername=this.hadleUsername.bind(this);
this.handlePass=this.handlePass.bind(this);
}
hadleUsername(e){
this.setState({username:e.target.value})
}
handlePass(e){
this.setState({password:e.target.value})
}
submit(e){
console.log(this.state.data);
if(this.state.data!=null){
this.state.data.results.map(data=>{
if(data.name===this.state.username && data.birth_year===this.state.password){
return <Redirect to='/info/'/>
}
});
}
}
async componentDidMount(){
const response = await fetch('https://swapi.co/api/people/');
const json = await response.json();
this.setState({data:json});
}
render(){
return(
<form className='login-container'>
<h3>Login Page</h3>
<label>Username:</label>
<input
type='text'
value={this.state.username}
placeholder='username'
onChange={this.hadleUsername}
/>
<br/><br/>
<label>Password:</label>
<input
type='password'
value={this.state.password}
placeholder='password'
onChange={this.handlePass}
/>
<br/><br/>
<button type='submit' onClick={this.submit.bind(this)}>submit</button>
</form>
)
}
}
Is there a better way to do it?
node.js reactjs react-native redirect render
node.js reactjs react-native redirect render
edited Nov 12 at 5:45
Peter Mortensen
13.4k1983111
13.4k1983111
asked Nov 11 at 10:23
rajatgalav
106
106
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247794%2fhow-to-use-redirect-in-react-js-with-fetching-data-from-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown