【问题标题】:How to pass route parameters from one Component and display in the other如何从一个组件传递路由参数并在另一个组件中显示
【发布时间】:2022-01-05 14:20:18
【问题描述】:

我对 React Router 的概念还很陌生,目前正在努力理解如何从一个组件和显示器传递数据或使用它在另一个组件上执行其他功能。我使用了useHistory 钩子和useParams 钩子。

这是我使用useHistory钩子的地方

const Table = (props) => {
    const data = [];
    const history = useHistory();

    const [tableData, setTableData] = useState(data)

    useEffect(() => {
        setTableData(props.buttonValue)
    }, [props.buttonValue]);

    const tableDataPage = (value) => {
        history.push(`/tableData/${value}`)
    };

    return (
        <div className="tableDataList">
            {!tableData ? "" : <table className="serviceRequestList">

                <tbody>
                    <tr>
                        <th style={{ width: "5%" }}>Serial Number<span><ImFilter size={"12px"} /></span></th>
                        <th>Name<span><ImFilter size={"12px"} /></span></th>
                        <th>Date<span><ImFilter size={"12px"} /></span></th>
                        <th>Issue<span><ImFilter size={"12px"} /></span></th>
                        <th>Status<span><ImFilter size={"12px"} /></span></th>
                    </tr>
                    {tableData.map((value, key) => (
                        <tr key={key} onClick={() => tableDataPage(value.serial_number)}>
                            <td>{value.serial_number}</td>
                            <td>{value.name}</td>
                            <td>{value.date}</td>
                            <td>{value.issue}</td>
                            <td>{value.status}</td>
                        </tr>
                    ))}
                </tbody>
            </table>}
        </div>
    );
}

export default Table;

我们被定向到的组件是下面的那个。但是,尽管路由链接中显示了 serial_number,但它不会显示在 divconsole.log 中。控制台日志显示一个未定义的值。

import { useParams } from 'react-router-dom';

const TableData = () => {
    const { serial_number } = useParams();
    console.log(serial_number);

    return (
        <div>Table Data - {serial_number}</div>
    );
}

export default TableData;

0073 是链接中的序列号,这是屏幕上表格数据文本旁边的预期值

这里是声明路由的 App.js 文件。

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

function App() {
  return (
    <Router>
      <div className="App">
        <Switch>
          <Route exact path="/" component={MainScreen}></Route>
          <Route path="/addData" component={AddDataScreen}></Route>
          // This is the route in question
          <Route path="/tableData/:id" component={TableData}></Route>
        </Switch>
      </div>
    </Router>
  );
}

export default App;

【问题讨论】:

  • 显示您是如何定义/tableData 路由的
  • @GabrielePetrioli 问题现已更新

标签: reactjs react-router


【解决方案1】:

所以在你的路由中,参数是:id,这就是你从参数中访问它的方式。

const { id } = useParams();

路由不关心参数来自哪里,它只知道动态部分命名为id

【讨论】:

  • 是的,这似乎有效。我认为它必须是从表组件中推送的值。我确实说过我对 React-Router 很陌生 :D :D 。不过还是谢谢你。
  • @coolhack7 在&lt;Route path="/tableData/:id" 定义路线时,您决定名称。我你把&lt;Route path="/tableData/:serial_number"放在那里,它可以与原始代码一起使用。使用对您和您的应用更有意义的命名。
猜你喜欢
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
相关资源
最近更新 更多