从react-router-dom转换为NextJS

时间:2020-01-27 09:14:07

标签: next.js react-router-dom

我不确定如何使用他们的路由器API而不是react-router-dom在NextJS中复制它。

async function getDataFromDB(){
var allPromise = [];
var i = 0;

let d1 = new Date;
let utc1 = d.getTime() + (d.getTimezoneOffset() * 60000);
let myDate1 = new Date(utc + (3600000*(+6.0)));
myDate1 = myDate1.toString();
return ref.on('value',function(snapshot){
    snapshot.forEach(function(childSnapshot){
        var childData = childSnapshot.val();
        allPromise.push(callbackFunction(childSnapshot.key, childData['ReceiptData'], myDate1));
    });
    Promise.all(allPromise)
    .then((result) =>{
        for (i = 0; i < allPromise.length; i++) {
            parseData(ids[i], result[i], myDate1);
        }
        return result;
    })
    .catch((err) =>{
        console.log("promise error :"+err);
    });
    console.log("done");

  });
}
let callbackFunction = function(userID, receiptData, myDate){
console.log("from callback");
return new Promise(function(resolve, reject){
    setTimeout(()=>{
        let password = 'my_password';
    const data = JSON.stringify({
            'receipt-data': receiptData,
            'password' : password,
            'exclude-old-transactions': false
        });
        const options = {
            resolveWithFullResponse: true,
            hostname: 'buy.itunes.apple.com',
            port: 443,
            path: '/verifyReceipt',
            method: 'POST',
            json: true,
            headers: {
                'Content-Type': 'application/json',
            }
        };
        try{
            const req = https.request(options, res => {
            myStatusCode[0] = res.statusCode;
            var body = '';

            console.log(`statusCode : ${res.statusCode} : ${userID}`);
            res.on('data', d => {
                body = body + d;                
            });
            res.on('end', () => {
                body = JSON.parse(body);
                console.log("body keys = " + body.keys);
                resolve(body);
            });
            res.on("error", err => {
                console.error("request error : "+err);
                reject(err);
            });
        });
        req.write(data);
        req.end();
        }catch(er){
            console.error("request error : "+ er);
        }
    });
  },1500);
} 

用法如下:

import { Route, Redirect } from "react-router-dom";
import auth from "../../services/authService";

const ProtectedRoute = ({ path, component: Component, render, ...rest }) => {
  return (
    <Route
      {...rest}
      render={props => {
        if (!auth.getCurrentUser())
          return (
            <Redirect
              to={{
                pathname: "/login",
                state: { from: props.location }
              }}
              j
            />
          );
        return Component ? <Component {...props} /> : render(props);
      }}
    />
  );
};

export default ProtectedRoute;

0 个答案:

没有答案