[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 arguement
const HomeLink = withProps(({ query }) => ({ href: ‘#/?query=‘ + query }))(‘a‘);
// take object as arguement
const 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.

时间: 2024-08-28 01:10:08

[Recompose] Lock Props using Recompose -- withProps的相关文章

[Recompose] Transform Props using Recompose --mapProps

Learn how to use the 'mapProps' higher-order component to modify an existing component’s API (its props). 'mapProps' takes incoming props and changes them however you’d like; for example, filtering the props by a field. For example, we have a UserLis

[Recompose] Stream Props to React Children with RxJS

You can decouple the parent stream Component from the mapped React Component by using props.children instead. This process involves mapping the stream to React’s cloneElement with the children then passing the props in manually. We have the code belo

[Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose

This lesson takes the concept of render props and migrates it over to streaming props by keeping the same example and simple refactoring the Togglecomponent which handles the render prop. const ToggleStream = componentFromStream(props$ => { const { h

[Recompose] Set the HTML Tag of a Component via a Prop using Recompose

Learn how to user the ‘componentFromProp’ helper and ‘defaultProps’ higher order component to swap the underlying html tag of your component. Sometimes we want a component to behave the same overall but to use a different element in the HTML output.

[React] Recompose: Theme React Components Live with Context

SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. Primary Color or Link Color). When we isolate our styles inside React components we lose the ability to theme them. To get round this we can put our th

[Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose

Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a class component. import React from 'react'; import { withReducer, withHandlers, compose, lifecycle } from 'recompose'; // Mock Configuration function fe

[Recompose] Add Local State with Redux-like Reducers using Recompose

Learn how to use the 'withReducer' higher order component using the alternative reducer form. If you like using reducers in redux, you’ll be able to reuse that same technique but for local state. import React from 'react'; import {withReducer, withHa

[Recompose] Add Local State to a Functional Stateless Component using Recompose

Learn how to use the 'withState' and 'withHandlers' higher order components to easily add local state to—and create a reusable local state pattern for—your functional stateless components. No need for classes! withState: const Statue = withState('sho

Java并发(2):Lock

在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方式来实现同步访问,那就是Lock. 也许有朋友会问,既然都可以通过synchronized来实现同步访问了,那么为什么还需要提供Lock?这个问题将在下面进行阐述.本文先从synchronized的缺陷讲起,然后再讲述java.util.concurrent.locks包下常用的有哪些类和接口,最后