首页 > 代码库 > [Recompose] Lock Props using Recompose -- withProps

[Recompose] Lock Props using Recompose -- withProps

Learn how to use the ‘withProps’ higher order component to pre-fill a prop, unable to be overridden.

 

const { Component } = React;const { withProps } = Recompose;// with function as arguementconst HomeLink = withProps(({ query }) => ({ href: #/?query= + query }))(a);// take object as arguementconst ProductsLink = withProps({ href: #/products })(a);const CheckoutLink = withProps({ href: #/checkout })(a);const App = () =>  <div className="App">    <header>      <HomeLink query="logo">Logo</HomeLink>    </header>    <nav>      <HomeLink>Home</HomeLink>      <ProductsLink>Products</ProductsLink>      <CheckoutLink>Checkout</CheckoutLink>    </nav>  </div>;ReactDOM.render(  <App />,  document.getElementById(main));

 

withProps, take string as arguement for creating a new DOM element.

[Recompose] Lock Props using Recompose -- withProps