代码之家  ›  专栏  ›  技术社区  ›  whitebear

将变量从路由路径传递到另一个路由路径?

  •  0
  • whitebear  · 技术社区  · 3 年前

    索引.js

    function start(){
      ReactDOM.render(
        <Router history={hist}>
          <Switch>
            <Route path="/making" component={MakingPage} />
            <Route path="/dashboard" render={() => <DashboardPage name={'Tom'}/>}/>
          </Switch>
        </Router>,
        document.getElementById("root")
      );
    }
    

    我可以给她 name 这样地。

    仪表板页面.js

    class DashboardPage extends React.Component {
      constructor(props) {
        super(props);
      }
      render(){
        console.log(this.props.name)// Tom comes here!
      } 
    }
    

    我可以把参数从 index.js DashboardPage.js

    现在我有了变量 myParam=1 在里面 MakingPage

    那我想搬到 dashboard myParam=1

    有可能吗?还是坏主意?

    export default function MakingPage(props) {
      function giveParam(){
         let mParam = 1;
         //I want to move /dashboard page and give the `myParam=1` to `dashboardPage` like `name={'Tom')`  
      }
      return (
    
      );
    }
    
    0 回复  |  直到 3 年前