简体   繁体   English

如何从 function React JS 打开不同的页面

[英]How to open different page from function React JS

I have two forms.我有两个 forms。

Login.jsx:登录.jsx:

import React, {Fragment, useState } from "react";
import axios from "axios";

export const Login = (props) => {
  const [user, setUser] = useState('');
  const [pass, setPass] = useState("");

  const handleSubmit = (e) => {
    e.preventDefault();
    const data = {
      KorisnickoIme: user,
      Lozinka: pass
    };

    const url ='https://localhost:44357/api/Fin/Login';
    axios.post(url, data).then((result)=>{
      alert(result.data);
    }).catch((error)=>{
      alert(error);
    })
  };

  return (
    <Fragment>
    <div className="auth-form-container">
      <h2>Пријава</h2>
      <form className="login-form" onSubmit={handleSubmit}>
      <label htmlFor="user">Корисничко ме</label>
            <input value ={user} onChange ={(e) => setUser(e.target.value)}placeholder="korisnicko_ime" id="user" name="user" required/> 

        <label htmlFor="email">Лозинка</label>
        <input
          value={pass}
          onChange={(e) => setPass(e.target.value)}
          type="password"
          placeholder="********"
        ></input>
        <button type="submit">Пријави се</button>
      </form>
      <button
        className="link-btn"
        onClick={() => props.onFormSwitch("register")}
      >
        Немаш налог? Региструј се овде.
      </button>
    </div>
    </Fragment>
  );
};


and Register.jxs:和 Register.jxs:

import React, {Fragment, useState} from "react";
import axios from "axios";

import { ProfileSetUp } from './ProfileSetUp';

export const Register = (props) => {
    const [email, setEmail] = useState('');
    const [pass, setPass] = useState('');
    const [name, setName] = useState('');
    const [user, setUser] = useState('');
    const [lastname, setLastname] = useState('');
    const [checkPass, setCheckPass] = useState('');

    const handleSubmit = (e) => {  
        if(pass==checkPass){
            e.preventDefault();
            const data = {
                KorisnickoIme:user,
                Ime: name,
                Prezime: lastname,
                Email: email,
                Lozinka: pass
            };

            const url ='https://localhost:44357/api/Fin/Registration';
            axios.post(url, data).then((result)=>{
                alert(result.data);
            }).catch((error)=>{
                alert(error);
            })
        }else{
            alert("Лозинке се не поклапају");
        }      

            
        }

    return(
        <Fragment>
        <div className="auth-form-container"> 
        <h2>Регистрација</h2>
        <form className="reg-form" onSubmit={handleSubmit}>
            <label htmlFor="name">Име</label>
            <input value ={name} onChange ={(e) => setName(e.target.value)}placeholder="Унесите Ваше име" id="name" name="name" required/>

            <label htmlFor="lastname">Презиме</label>
            <input value ={lastname} onChange ={(e) => setLastname(e.target.value)}placeholder="Унесите Ваше презиме" id="lastname" name="lastname" required/>

            <label htmlFor="email">Е-маил</label>
            <input value ={email} onChange ={(e) => setEmail(e.target.value)}type="email" placeholder="vasmail@fink.com" id="email" name="email" required/>

            <label htmlFor="user">Корисничко ме</label>
            <input value ={user} onChange ={(e) => setUser(e.target.value)}placeholder="Унесите Ваше korisnicko_ime" id="user" name="user" required/> 

            <label htmlFor="password">Лозинка</label>
            <input value ={pass} onChange ={(e) => setPass(e.target.value)}type="password" placeholder="********" id="password" name="password" required/>

            <label htmlFor="checkPass">Потврдите лозинку</label>
            <input value ={checkPass} onChange ={(e) => setCheckPass(e.target.value)}type="password" placeholder="********" id="checkPass" name="checkPass" required/>

            <button type="submit">Региструј се</button>
        </form>
        <button className="link-btn" onClick={() => props.onFormSwitch('login')}>Имате налог? Пријавите се.</button>
        </div>
        </Fragment>
    )
}

/*  */

After registering I want to send user to Login form but I am not sure how should I do it.注册后我想将用户发送到登录表单,但我不知道该怎么做。 Here is App.js just in case:这是 App.js 以防万一:


import React, {useState} from 'react';
import './App.css';
import { Register } from './Register';
import { Login } from './Login';
import { ProfileSetUp } from './ProfileSetUp';

function App() {
  const [currentForm, setCurrentForm] = useState('login');

  const toggleForm = (formName) => {
    setCurrentForm(formName);
  }



  return (
    <div className="App">
      {

        currentForm === "login" ? <Login onFormSwitch={toggleForm} /> : <Register onFormSwitch={toggleForm} />
      }
    </div>
  );
}
export default App;

I am not sure how React page is represented so I am not sure how to use window.open() etc. I followed one tutorial where is shown how to make this toggle switch function, which I like but I think that will make me troubles with other pages.我不确定 React 页面是如何表示的,所以我不确定如何使用 window.open() 等。我遵循了一个教程,其中显示了如何制作此切换开关 function,我喜欢它,但我认为这会让我遇到麻烦与其他页面。

You can use the react-router package to link between different pages and pass data.您可以使用 react-router package 在不同页面之间进行链接并传递数据。 Here is the documentation for the package https://reactrouter.com/en/main .这是 package https://reactrouter.com/en/main的文档。

If you don't want to navigate to different components using routing, you could just use state variables to show/hide components included in a single parent component.如果您不想使用路由导航到不同的组件,您可以只使用 state 变量来显示/隐藏包含在单个父组件中的组件。

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

相关问题 在Node.js中更改为其他页面/组件时,如何运行其他SQL函数(反应)? - How to run a different SQL function when changing to a different page/component in Node.js (React)? reactjs js如何从内容的子链接打开新页面而不显示上一页 - react js How to open new page from child link on content without show previous page 如何使用 react js 为不同的页面使用不同的 CSS 文件 - How to use different CSS files for different page with react js 在 React 中,当单击来自不同功能组件的按钮时如何打开下拉列表 - In React how open the dropdown when click a button from different function component 如何从功能中打开组件React Native - How to open a component from a function react native 如何使用 React.js 从不同页面的表单中添加表中的输入值? - How to add input values in table from a form in different page using React.js? 如何使用 puppeteer 在不同的异步函数中打开相同的页面? - how to get the same page to open in different async function using puppeteer? 如何让一个 JS 函数打开一个新的 HTML 页面然后运行? - How to have a JS function open a new HTML page and then run? 从其他页面打开模式 - Open modal from a different page 如何从js函数调用react组件 - how to call react component from js function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM