React快速调试的小技巧:直接在组件上输出props/state

以react route为例

父组件App

const App = () => (
  <BrowserRouter>
    <div className="app">
      <Switch>
        <Route exact path="/" component={Landing} />
        <Route path="/search" component={Search} />
        <Route path="/details/:id" component={Details} />
        <Route component={FourOhFour} />
      </Switch>
    </div>
  </BrowserRouter>
);

Details组件

const Details = props => (
  <div className="details">
    <pre>
      <code>
        {JSON.stringify(props, null, 4)}
      </code>
    </pre>
  </div>
);

输出效果

 

posted on 2019-10-16 04:05  z_13  阅读(607)  评论(0)    收藏  举报

导航