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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 5:45









      Peter Mortensen

      13.4k1983111




      13.4k1983111










      asked Nov 11 at 10:23









      rajatgalav

      106




      106





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values