我想在路线内打开一个弹出窗口,并想在网址中添加一个哈希。
例如onClick public void run() {
mmBuffer = new byte[1024];
int numBytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs.
while (true) {
try {
// Read from the InputStream.
numBytes = mmInStream.read(mmBuffer);
// Send the obtained bytes to the UI activity.
Message readMsg = handler.obtainMessage(
MessageConstants.MESSAGE_READ, numBytes, -1,
mmBuffer);
readMsg.sendToTarget();
} catch (IOException e) {
Log.d(TAG, "Input stream was disconnected", e);
break;
}
}
之后onClick https://www.example.com/home
效果很好,但反应路由器重新渲染了整个路线。
我在React Router上做错什么了吗?让我们在下面看我的代码(我简化了事情)
https://www.example.com/home#send-me-an-email
我和所有人一样都在使用BrowserRouter
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';
const render = Component => {
ReactDOM.render(
<Router>
<Component />
</Router>,
document.getElementById('root')
);
};
render(App);
我正在使用withRouter,因为我需要其他位置的历史记录和位置
App.jsx
import React from 'react';
import { Route, Switch, withRouter } from 'react-router-dom';
import Header from './components/Header';
import Footer from './components/Footer';
import Home from './views/Home';
const App = ({ ...props }) => {
return (
<Header />
<section>
<Switch>
<Route exact path={"/"} component={() => <Home {...props} />} />
<Route path={"/home"} component={() => <Home {...props} />} />
</Switch>
</section>
<Footer />
);
};
export default withRouter(App);
不幸的是,当我执行Home.jsx
时,它将重新渲染路线组件Home,不好了
this.props.history.push({ hash: 'send-me-an-email' })
如何不仅仅因为我在同一个URL上添加了哈希值就进行了重新渲染?干杯。
答案 0 :(得分:0)
仅依靠香草JS:
window.history.pushState("object or string", "Title", "/home#send-me-an-email");
这将添加哈希/路由,而无需渲染或重新加载任何内容。