gpt4 book ai didi

javascript - 将react-router-dom链接传递到外部库

转载 作者:行者123 更新时间:2023-12-03 13:20:42 24 4
gpt4 key购买 nike

我正在从外部(node_modules)模式库渲染组件。在我的主应用程序中,我将 Link 实例从 react-router-dom 传递到我的外部库的组件中,如下所示:

import { Link } from 'react-router-dom';
import { Heading } from 'my-external-library';

const articleWithLinkProps = {
url: `/article/${article.slug}`,
routerLink: Link,
};

<Heading withLinkProps={articleWithLinkProps} />

在我的库中,它将 Link 呈现如下:

const RouterLink = withLinkProps.routerLink;

<RouterLink
to={withLinkProps.url}
>
{props.children}
</RouterLink>

RouterLink 似乎能够正确呈现,甚至在单击时导航到该 URL。

<小时/>

我的问题RouterLink 似乎已与我的应用程序的 react-router-dom 实例分离。当我点击 Heading 时,它会“硬”导航,回发页面,而不是像 Link 通常那样无缝路由到那里。

我不确定此时应该尝试什么才能让它无缝导航。任何帮助或建议将不胜感激,提前谢谢您。

编辑:显示我的路由器的设置方式。

import React from 'react';
import { hydrate, unmountComponentAtNode } from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { Provider } from 'react-redux';
import { createBrowserHistory } from 'history';
import { ConnectedRouter } from 'react-router-redux';
import RedBox from 'redbox-react';
import { Route } from 'react-router-dom';
import { Frontload } from 'react-frontload';
import App from './containers/App';

import configureStore from './redux/store';
import withTracker from './withTracker';

// Get initial state from server-side rendering
const initialState = window.__INITIAL_STATE__;
const history = createBrowserHistory();
const store = configureStore(history, initialState);
const mountNode = document.getElementById('react-view');
const noServerRender = window.__noServerRender__;

if (process.env.NODE_ENV !== 'production') {
console.log(`[react-frontload] server rendering configured ${noServerRender ? 'off' : 'on'}`);
}

const renderApp = () =>
hydrate(
<AppContainer errorReporter={({ error }) => <RedBox error={error} />}>
<Provider store={store}>
<Frontload noServerRender={window.__noServerRender__}>
<ConnectedRouter onUpdate={() => window.scrollTo(0, 0)} history={history}>
<Route
component={withTracker(() => (
<App noServerRender={noServerRender} />
))}
/>
</ConnectedRouter>
</Frontload>
</Provider>
</AppContainer>,
mountNode,
);

// Enable hot reload by react-hot-loader
if (module.hot) {
const reRenderApp = () => {
try {
renderApp();
} catch (error) {
hydrate(<RedBox error={error} />, mountNode);
}
};

module.hot.accept('./containers/App', () => {
setImmediate(() => {
// Preventing the hot reloading error from react-router
unmountComponentAtNode(mountNode);
reRenderApp();
});
});
}

renderApp();

最佳答案

我已经在 codesandbox.io 中重建了您的用例,并且“转换”工作正常。所以也许检查我的实现可能会对您有所帮助。但是,我用文件导入替换了库导入,所以我不知道这是否是它在不重新加载整个页面的情况下无法工作的决定性因素。

顺便问一下,“无缝”到底是什么意思?是否有一些元素保留在每个页面上并且在单击链接时不应再次重新加载?这就像我在沙箱中实现它一样,其中静态图片位于每个页面的顶部。

<小时/>

查看 sandbox .

这是example.js文件

// This sandbox is realted to this post https://stackoverflow.com/q/59630138/965548

import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import { Heading } from "./my-external-library.js";

export default function App() {
return (
<div>
<img
alt="flower from shutterstock"
src="https://image.shutterstock.com/image-photo/pink-flowers-blossom-on-blue-600w-1439541782.jpg"
/>
<Router>
<Route exact={true} path="/" render={Welcome} />
<Route path="/article/coolArticle" component={CoolArticleComponent} />
</Router>
</div>
);
}

const Welcome = () => {
const articleWithLinkProps = {
url: `/article/coolArticle`,
routerLink: Link
};

return (
<div>
<h1>This is a super fancy homepage ;)</h1>
<Heading withLinkProps={articleWithLinkProps} />
</div>
);
};

const CoolArticleComponent = () => (
<div>
<p>This is a handcrafted article component.</p>
<Link to="/">Back</Link>
</div>
);

这是 my-external-library.js 文件:

import React from "react";

export const Heading = ({ withLinkProps }) => {
const RouterLink = withLinkProps.routerLink;
return <RouterLink to={withLinkProps.url}>Superlink</RouterLink>;
};

关于javascript - 将react-router-dom链接传递到外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59630138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com