React Router更改链接但不会呈现正确的组件

时间:2017-08-05 15:18:50

标签: reactjs react-router react-router-v4

您好我正在使用React Router ...我有两个组件App.js和SideNavComponent。我想单击SideNavComponent中的链接并前往正确的Component。

现在,当我点击链接时,它会更改URL,但不会呈现正确的组件。但是,如果我自己更改URL,则会显示正确的视图。

App.js

import React, { Component } from 'react';
import { Col, Well, Panel } from 'react-bootstrap';

import NavbarComponent from './NavbarComponent';
import SideNavComponent from './SideNavComponent';
import Router from './Router';

class App extends Component {
  render() {
    return (
      <div>
        <div className="header">
          <NavbarComponent />
        </div>

        <div className="body">
          <Col md={3}>
            <Well>
              <SideNavComponent />
            </Well>
          </Col>

          <Col md={9}>
            <Panel>
              <Router />
            </Panel>
          </Col>
        </div>

        <div className="footer" />
      </div>
    );
  }
}

export default App;

SideNavComponent

import React, { Component } from 'react';
import { Nav, NavItem } from 'react-bootstrap';
import { Link, BrowserRouter, Switch } from 'react-router-dom';

class SideNavComponent extends Component {
  state = {
    selectedLink: 1
  };

  selectLink(key) {
    this.setState({ selectedLink: key });
  }

  render() {
    return (
        <BrowserRouter>
          <div style={{ flexDirection: 'column' }}>
              <Link to="/">Home</Link>
              <Link to="/device">Device</Link>
              <Link to="/transform">Transformation</Link>
              <Link to="/graph">Graphs</Link>
          </div>
        </BrowserRouter>
    );
  }
}

export default SideNavComponent;

Router.js

import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';

import HomeComponent from './pages/HomeComponent';
import DeviceComponent from './pages/DeviceComponent';
import TransformationComponent from './pages/TransformationComponent';
import GraphComponent from './pages/GraphComponent';

const Router = () => {
  return (
    <BrowserRouter>
      <div>
        <Switch>
          <Route path="/device" component={DeviceComponent} />
          <Route path="/transform" component={TransformationComponent} />
          <Route path="/graph" component={GraphComponent} />
          <Route exact path="/" component={HomeComponent} />
        </Switch>
      </div>
    </BrowserRouter>
  );
};

export default Router;

1 个答案:

答案 0 :(得分:1)

问题是你有多个浏览器路由器实例并且历史记录变得混乱。您可以通过重新组织应用程序的结构来解决此问题,如下例所示。

+ (NSString *) getEncryptedValueWithKey:(NSString *)theKey forString:(NSString *)theString
{
    NSData *rawData = [theString dataUsingEncoding:NSUTF8StringEncoding];

    unsigned char md5Buffer[kCCKeySizeAES256+1];
    memset(md5Buffer, 0, kCCKeySizeAES256+1);
    const char *cStr = [theKey UTF8String];

    // do md5 hashing
    CC_MD5(cStr, CC_MD5_DIGEST_LENGTH, md5Buffer);

    unsigned char lastChar = md5Buffer[15];
    for (NSInteger i = 15; i <= 30; i++) {
        md5Buffer[i] = md5Buffer[i-15];
    }
    md5Buffer[30] = lastChar;
    //MD5 key obtaining end

    NSUInteger dataLength = [rawData length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding + kCCOptionECBMode, md5Buffer, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [rawData bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted);
    if (cryptStatus == kCCSuccess)
    {
        NSData *tempData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        NSString* encrypted64 = [tempData base64EncodedStringWithOptions:0];//Even i have tried base 64 encding with other options available
        return encrypted64;
    }

    free(buffer); //free the buffer;
    return nil;
}