1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

React Router v6 でページ遷移で props を渡す

Posted at

初めに

react router v6 で props の渡し方について調べました。

以下に記載があります。

例を作りました。

Qiita-gif用 (2).gif

App.js
import "./styles.css";
import { useState } from "react";
import { BrowserRouter, Routes, Route, useNavigate } from "react-router-dom";

const Count = (props) => {
  // props を受け取る
  const { count, setCount } = props;
  return (
    <div>
      <h3>Click this button</h3>
      <button onClick={() => setCount(count + 1)}>Click</button>
      <h6>COUNT is {count}</h6>
    </div>
  );
};

const Home = () => {
  const navigate = useNavigate();

  return (
    <div>
      <h1>Home</h1>
      <button onClick={() => navigate("countpage")}>Go to count page</button>
    </div>
  );
};

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <div className="App">
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route
            path="/countpage"
            // prors を渡す
            element={<Count count={count} setCount={setCount} />}
          />
        </Routes>
      </BrowserRouter>
    </div>
  );
}

参考記事

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?