【问题标题】:Navigate from one page to another on Button click单击按钮从一个页面导航到另一个页面
【发布时间】:2021-08-19 15:53:40
【问题描述】:

嘿,我正在尝试制作一个学生管理系统,我在其中制作了一个登录页面、注册页面和一个仪表板页面。 现在,从我的仪表板页面单击“填充详细信息”按钮时,我需要导航到另一个页面 input.js(这实际上是一个单独的项目)。我将在下面链接我的代码。请帮助我完成它!

//input.js
    import React, { Component } from 'react';
    import web3 from './web3';
    import ipfs from './ipfs';
    import storehash from './storehash';
    import { Button } from 'reactstrap';


    class MyComponent extends Component {
      state = {      
        ipfsHash:null,      
        buffer:'',      
        ethAddress:'',      
        transactionHash:'',      
        txReceipt: ''    
      };


      //Take file input from user
      captureFile =(event) => {event.stopPropagation() 
        event.preventDefault() 
        const file = event.target.files[0] 
        let reader = new window.FileReader() 
        reader.readAsArrayBuffer(file) 
        reader.onloadend = () => this.convertToBuffer(reader) };


      //Convert the file to buffer to store on IPFS 
          convertToBuffer = async(reader) => {

      //file is converted to a buffer for upload to IPFS        
          const buffer = await Buffer.from(reader.result);   

      //set this buffer-using es6 syntax        
           this.setState({buffer});};

      //ES6 async 
        functiononClick = async () => {try{this.setState({blockNumber:"waiting.."});        
        this.setState({gasUsed:"waiting..."});
        await web3.eth.getTransactionReceipt(this.state.transactionHash, (err, txReceipt)=>{          
          console.log(err,txReceipt);          
          this.setState({txReceipt});        
        });    
      }

      catch(error){      
        console.log(error);   
       }}


       onSubmit = async (event) => {      
         event.preventDefault();

         //bring in user's metamask account address      
         const accounts = await web3.eth.getAccounts();   

         //obtain contract address from storehash.js      
         const ethAddress= await storehash.options.address;      
         this.setState({ethAddress});    

         //save document to IPFS,return its hash#, and set hash# to state      
         await ipfs.add(this.state.buffer, (err, ipfsHash) => {        
           console.log(err,ipfsHash);        

           //setState by setting ipfsHash to ipfsHash[0].hash        
           this.setState({ ipfsHash:ipfsHash[0].hash });       
           
           
           // call Ethereum contract method "sendHash" and .send IPFS hash to etheruem contract       
          //return the transaction hash from the ethereum contract        
          storehash.methods.sendhash(this.state.ipfsHash).send({          
            from: accounts[0]        
          }, 
            (error, transactionHash) => {          
            console.log(transactionHash);          
            this.setState({transactionHash});        
          });  

          

        })    
      };


      render() {
        return (        
        <div className="App">          
        <header className="App-header">  
        <h1>EduDecentro</h1>          
        </header>

        <hr/>
        <grid>          
          <h5> Choose Transcript file  </h5>          
          <form onSubmit={this.onSubmit}>            
          <input              
          type = "file"              
          onChange = {this.captureFile}            
          />             
          <Button             
          bsStyle="primary"             
          type="submit">             
          Send it             
          </Button> 
          </form>
          <tbody>                 
             <tr>                   
               <td>IPFS Hash</td>                   
                <td> : </td>                    
                <td>{this.state.ipfsHash1}</td>                  
                </tr>
          </tbody>
          

          <h5> Choose Certificate-1 file  </h5>          
          <form onSubmit={this.onSubmit}>            
          <input              
          type = "file"              
          onChange = {this.captureFile}            
          />             
          <Button             
          bsStyle="primary"             
          type="submit">             
          Send it             
          </Button> 
          </form>
          <tbody>                 
             <tr>                   
               <td>IPFS Hash</td>                   
                <td> : </td>                    
                <td>{this.state.ipfsHash2}</td>                  
                </tr>
          </tbody>

          <h5> Choose Certificate-2 file  </h5>          
          <form onSubmit={this.onSubmit}>            
          <input              
          type = "file"              
          onChange = {this.captureFile}            
          />             
          <Button             
          bsStyle="primary"             
          type="submit">             
          Send it             
          </Button> 
          </form>

          <h5> Choose Resume file  </h5>          
          <form onSubmit={this.onSubmit}>            
          <input              
          type = "file"              
          onChange = {this.captureFile}            
          />             
          <Button             
          bsStyle="primary"             
          type="submit">             
          Send it             
          </Button> 
          

          </form><hr/> 
          </grid>     
        </div>      
       );    
    }}

     export default MyComponent;
    import React from 'react';


    import {Link } from "react-router-dom";

     
    function Dashboard(props) {
     
      // handle click event of logout button
      const handleLogout = () => {    
        props.history.push('/Sign-in');
      }

      
     
      return (
        <div>
          Welcome User!<br /><br />
          <Link to="/input"><button>
                  Fill Details 
                </button>
                </Link>

          <input type="button" onClick={handleLogout} value="Logout" />
        </div>
      );
    }
     
    export default Dashboard;

【问题讨论】:

  • 使用来自'react-router-dom'的重定向组件
  • 能否详细说明!
  • 欢迎来到 StackOverflow!如果您提供minimal reproducible example,您更有可能收到有用的答案。

标签: javascript html react-router router


【解决方案1】:

您所要做的就是为您的Fill Details 按钮添加事件监听器,您不需要将它包装到Link 组件中。示例如下:

<button onClick={() => window.location.href = 'YOUR_LINK_TO_ANOTHER_APP' }>
  Fill Details 
</button>

【讨论】:

  • 实际上我将两个应用程序放在单独的文件夹中,我需要将仪表板页面导航到另一个反应应用程序(这是一个单独的文件夹)。我该怎么做? 这种情况下也可以吗?或者我怎样才能将两个应用程序组合成一个应用程序!单个应用会更好。请提出一些想法
  • 你试过我的方法了吗?
  • 你在window.location.href放了什么链接?
  • 就是,'/input'。
  • 如果您想导航到另一个应用程序,则 URL 必须是绝对的,例如 www.google.com。仅插入 /input 您正在尝试导航到您的第一个应用程序中的页面,但事实并非如此。您的第二个应用程序必须部署在某处或至少在本地服务器上运行
【解决方案2】:
<button onClick="window.location.href='Second_App_Page_Link';">Click Here
</button>

【讨论】:

  • 实际上我将两个应用程序放在单独的文件夹中,我需要将仪表板页面导航到另一个反应应用程序(这是一个单独的文件夹)。我该怎么做? 这种情况下也可以吗?或者我怎样才能将两个应用程序组合成一个应用程序!单个应用会更好。请提出一些想法
  • 你的意思是你在一个项目中有两个应用程序?或者您有 2 个应用程序根本没有连接?
  • 它们没有连接。两者都是使用 create-react-app 创建的独立应用程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2016-02-08
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多