higher-order-components

How can I create shared state for HOC?

断了今生、忘了曾经 提交于 2019-12-06 12:49:26
问题 I have created LoadBookHOC which is wrapped with BookDetails and BookSummary component. LoadBookHOC.js const LoadBookHOC = InnerComponent => class LoadBook extends React.Component { constructor(){ super(); this.state={ book: [] }; } set= obj => this.setState(obj); render(){ return( <InnerComponent {...this.props} hocState={this.state} hocSetState={this.set} /> ); } } BookDetails.js this.hocSetState({book: <new data>}); BookSummary.js this.hocSetState({book: <new data>}); Whenever this.props

Writing a React higher-order component with TypeScript

好久不见. 提交于 2019-12-04 19:37:24
问题 I'm writing a React higher-order component (HOC) with TypeScript. The HOC should accept one more prop than the wrapped component, so I wrote this: type HocProps { // Contains the prop my HOC needs thingy: number } type Component<P> = React.ComponentClass<P> | React.StatelessComponent<P> interface ComponentDecorator<TChildProps> { (component: Component<TChildProps>): Component<HocProps & TChildProps>; } const hoc = function<TChildProps>(): (component: Component<TChildProps>) => Component

How do I set PropTypes for Higher Order Functional Component?

只谈情不闲聊 提交于 2019-12-04 18:54:19
问题 I'm using airbnb configuration for eslint and it's giving me this warning: [eslint] 'isLoading' is missing in props validation (react/prop-types) Is there a way to set PropTypes for isLoading? const withLoading = Component => ({ isLoading, ...rest }) => (isLoading ? <LoadingSpinner /> : <Component {...rest} />); Here's an example of how I use it: const Button = ({ onClick, className, children }) => ( <button onClick={onClick} className={className} type="button"> {children} </button> ); Button

How can I create shared state for HOC?

怎甘沉沦 提交于 2019-12-04 16:45:19
I have created LoadBookHOC which is wrapped with BookDetails and BookSummary component. LoadBookHOC.js const LoadBookHOC = InnerComponent => class LoadBook extends React.Component { constructor(){ super(); this.state={ book: [] }; } set= obj => this.setState(obj); render(){ return( <InnerComponent {...this.props} hocState={this.state} hocSetState={this.set} /> ); } } BookDetails.js this.hocSetState({book: <new data>}); BookSummary.js this.hocSetState({book: <new data>}); Whenever this.props.hocState.book called in BookDetails or BookSummary , I need to get the same data. I mean all the

TypeScript: remove key from type/subtraction type

强颜欢笑 提交于 2019-12-04 16:39:33
问题 I want to define a generic type ExcludeCart<T> that is essentially T but with a given key (in my case, cart ) removed. So, for instance, ExcludeCart<{foo: number, bar: string, cart: number}> would be {foo: number, bar: string} . Is there a way to do this in TypeScript? Here's why I want to do this, in case I'm barking up the wrong tree: I'm converting an existing JavaScript codebase to TypeScript, which contains a decorator function called cartify that takes a React component class Inner and

Defining and exporting HOC in React

不想你离开。 提交于 2019-12-04 12:19:06
问题 I've been research Higher Order Components in react. My requirement is that I have a set components which I need to extend to give them more functionality without rewriting the entire component. In this case, I found out the concept HOC in react where one could extend the component using a pure function. My question is, can I export the extended component as a normal component. For an example Component which needs to be extended class foo extends React.Component { render(){ //something } }

How to bind to and use a higher-order component in ReasonReact

廉价感情. 提交于 2019-12-04 09:44:00
Let's say I have a higher-order component, something like the following trivial definition, exported from the JavaScript module ./hoc.js : export const withStrong = Component => props => <strong> <Component ...props/> </strong> Assuming I have some component called HelloMessage , what is the equivalent of this piece of JavaScript: import { withStrong } from './hoc.js'; const HelloMessage = ... const StrongMessage = withStrong(HelloMessage); ReactDOM.render( <StrongMessage name="Joe" />, document.getElementById('react-app') ); TL;DR: This should be the exact equivalent of the requested

recompose withState how to update on props changes

风流意气都作罢 提交于 2019-12-04 04:50:17
问题 I have a higher order component that I try to modify a bit (I'm not familiar with recompose). So thats my component: <Map mycenter={mycenter} /> I want the map-component to update or to rerendered if the mycenter is updated. I'm trying to modify the code from https://github.com/istarkov/google-map-thousands-markers/blob/master/src/Map.js I made some modifications to the code. First, the map center is set to mycenter. That works. withState('mapParams', 'setMapParams', ({ mycenter }) => ({

How do I set PropTypes for Higher Order Functional Component?

大城市里の小女人 提交于 2019-12-03 12:21:36
I'm using airbnb configuration for eslint and it's giving me this warning: [eslint] 'isLoading' is missing in props validation (react/prop-types) Is there a way to set PropTypes for isLoading? const withLoading = Component => ({ isLoading, ...rest }) => (isLoading ? <LoadingSpinner /> : <Component {...rest} />); Here's an example of how I use it: const Button = ({ onClick, className, children }) => ( <button onClick={onClick} className={className} type="button"> {children} </button> ); Button.propTypes = { onClick: PropTypes.func.isRequired, className: PropTypes.string, children: PropTypes

Defining and exporting HOC in React

回眸只為那壹抹淺笑 提交于 2019-12-03 07:44:35
I've been research Higher Order Components in react. My requirement is that I have a set components which I need to extend to give them more functionality without rewriting the entire component. In this case, I found out the concept HOC in react where one could extend the component using a pure function. My question is, can I export the extended component as a normal component. For an example Component which needs to be extended class foo extends React.Component { render(){ //something } } export default foo; HOC component function bar(foo) { render() { return <foo {...this.props} {...this