简体   繁体   English

我正在使用 React Route,并且我有一个随机字符串,但是当我单击链接页面时有效,但是当再次单击时不起作用

[英]I'm working with React Route, and I have a random string, but when I do click on the link page works but when a do click again doesn't work

I have a simple menu and I have a page with a random context only for tests.我有一个简单的菜单,并且我有一个带有随机上下文的页面,仅用于测试。 Here my index.js这是我的 index.js

const Route       = ReactRouterDOM.Route;
const Link        = ReactRouterDOM.Link;
const HashRouter  = ReactRouterDOM.HashRouter;
const Routes      = ReactRouterDOM.Routes;

// create context
const UserContext = React.createContext(null);

function Spa() {

    return (
    <HashRouter>
    <div>
    <h1>Routing - Hello World</h1>
    <Link to="/">Home</Link> --
    <Link to="/about/">About</Link> --
    <Link to="/products">Products</Link>
    <hr />
    <UserContext.Provider value={{ users: ["peter"] }}>
    <Routes>
    <Route path="/" exact element={ <Home />} />
    <Route path="/about/" element={ <About />} />
    <Route path="/products/" element={ <Products />} />
    </Routes>
    </UserContext.Provider>
    </div>
    </HashRouter>
    );
    }

    ReactDOM.render(
    <Spa/>,
    document.getElementById('root')
    ); 

and this is my products.js这是我的 products.js

    const ctx = React.useContext(UserContext);  
    ctx.users.push(Math.random().toString(36).substr(2, 5));

    return (
    <div>
    <h3>Products Component</h3>
    <p>List of the the product we make</p>
    {JSON.stringify(ctx.users)}
    </div>
   );
} 

my problem is that when I do click on the products menu the fist time the random works, but if I do again doesn't works, I have to change of link, for example login and return again to products for random works.我的问题是,当我第一次点击产品菜单时,随机作品,但如果我再次点击不起作用,我必须更改链接,例如登录并再次返回产品以进行随机作品。

Someone knows what could be the problem?有人知道可能是什么问题吗?

I am using React-dom-16.8 History-5 React-Router-dom-6我正在使用 React-dom-16.8 History-5 React-Router-dom-6

Use BrowserRouter instead of HashRouter使用BrowserRouter而不是HashRouter

Issues问题

  1. Linking to a route with the same path and state/etc is effectively a non-op.链接到具有相同路径和状态/等的路由实际上是非操作。 It won't trigger a navigation and it won't trigger the routed component to rerender.它不会触发导航,也不会触发路由组件重新呈现。
  2. ctx.users.push(Math.random().toString(36).substr(2, 5)); is an unintentional side-effect and also won't trigger a rerender.是无意的副作用,也不会触发重新渲染。 You only see the mutation after navigating away and back to "/products" .您只能在导航离开并返回"/products"后看到突变。

Solution解决方案

  1. users should reside in some react state so when it's updated it can trigger React components to rerender. users应该驻留在一些react state 中,因此当它更新时,它可以触发 React 组件重新渲染。 Pass the users state and the state updater ( or some callback to update state ) as the UserContext value.users statestate 更新程序(或一些回调以更新 state )作为UserContext值传递。
  2. Pass some "random" state value in the link such that it will trigger the navigation action.在链接中传递一些“随机” state 值,以便触发导航操作。 Check for state in an useEffect to help trigger the effect to update the users state.在 useEffect 中检查useEffect以帮助触发更新users state 的效果。

Suggestions建议

Spa温泉

const Spa = () => {
  const [users, setUsers] = useState(["peter"]);
  return (
    <Router>
      <div>
        <h1>Routing - Hello World</h1>
        <Link to="/">Home</Link> --
        <Link to="/about/">About</Link> --
        <Link
          to="/products"
          state={{ value: Math.random() }} // <-- random state to trigger navigation
        >
          Products
        </Link>
        <hr />
        <UserContext.Provider value={{ users, setUsers }}>
          <Routes>
            <Route path="/" exact element={<Home />} />
            <Route path="/about/" element={<About />} />
            <Route path="/products/" element={<Products />} />
          </Routes>
        </UserContext.Provider>
      </div>
    </Router>
  );
};

Products产品

const Products = () => {
  const { state } = useLocation();
  const { users, setUsers } = useContext(UserContext);

  // "Listen" for updates to route state to trigger effect
  useEffect(() => {
    setUsers((users) => [...users, Math.random().toString(36).substr(2, 5)]);
  }, [state, setUsers]);

  return (
    <div>
      <h3>Products Component</h3>
      <p>List of the the product we make</p>
      {JSON.stringify(users)}
    </div>
  );
};

编辑 im-working-with-react-route-and-i-have-a-random-string-but-when-i-do-click-on

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 链接到另一个页面,当我单击 img 时无效,但当我单击文本时有效 - link to another page not working for when i click on an img but works when i click on a text 当我单击文本时它起作用,但是当我在文本旁边单击时,它在下拉菜单中不起作用? - When i click into text it works but when i click beside text it doesn't work in dropdown menu? 当我单击它时,React-router-dom 链接不起作用 - React-router-dom Link doesn't work when I click on it 当我在 React 中单击链接组件时如何重新加载页面? - How I reload the page when I click Link component in react? 如何在jquery中再次单击时显示DIV? - How do I show DIV when click again in jquery? 当我点击指向子页面的链接时,如何在页面页脚中保留一个不重新加载的音乐播放器? - How can I keep a music player in the page footer that doesn't reload when I click a link to a subpage? 单击<a>链接</a>时如何关闭导航栏<a>?</a> - How do i get the navbar to close when i click on a <a> link? 单击链接时如何显示字段? (javascript) - How do I display field when I click on a link? (javascript) 当.click()不起作用时,如何以编程方式单击此页面上的按钮? - How do I programmatically click a button on this page when .click() does not work? 我正在尝试做一个汉堡菜单,但当我点击它时它不会显示 - I'm trying to do a Hamburger Menu, but it won't show up when I click it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM