higher-order-components

HOC - Functional Component

我们两清 提交于 2020-11-30 09:10:12
问题 I have already created a HOC in my react app following this, and its working fine. However i was wondering if there is a way to create a HOC as functional component(With or without state)??? since the given example is a class based component. Tried to find the same over web but couldn't get anything. Not sure if thats even possible?? Or right thing to do ever?? Any leads will be appreciated :) 回答1: Definitely you can create a functional stateless component that accepts component as an input

Next.js Fetch data in HOC from server in SSG

泪湿孤枕 提交于 2020-08-27 06:38:59
问题 I created new app with Next.js 9.3.1 . In old app with SSR. I can you getInitialProps function in HOC components (not in page), so I can fetch data from server in HOC component and from page. Like this https://gist.github.com/whoisryosuke/d034d3eaa0556e86349fb2634788a7a1 Example : export default function withLayout(ComposedComponent) { return class WithLayout extends Component { static async getInitialProps(ctx) { console.log('ctxlayout fire'); const { reduxStore, req } = ctx || {} const

Next.js Fetch data in HOC from server in SSG

末鹿安然 提交于 2020-08-27 06:38:01
问题 I created new app with Next.js 9.3.1 . In old app with SSR. I can you getInitialProps function in HOC components (not in page), so I can fetch data from server in HOC component and from page. Like this https://gist.github.com/whoisryosuke/d034d3eaa0556e86349fb2634788a7a1 Example : export default function withLayout(ComposedComponent) { return class WithLayout extends Component { static async getInitialProps(ctx) { console.log('ctxlayout fire'); const { reduxStore, req } = ctx || {} const

Using withStyles with ag-grid custom cellEditor throws warning in console - Framework component is missing the method getValue()

爷,独闯天下 提交于 2020-05-17 05:55:28
问题 I am trying to implement custom datepicker as cellEditor to ag-grid column of type date. When I use plain class component it works fine. But the moment I wrap it within a HoC, specifically withStyles of Material-ui, I receive a warning in console "Framework component is missing the method getValue()". I tried to follow the same sample (without required change for datepicker) given in this link -https://www.ag-grid.com/react-hooks/. But it doesn't work as well. And I receive the same warning

Typescript `typeof React.Component` does not extend interface? Trouble defining higher-order component

核能气质少年 提交于 2020-02-04 05:25:07
问题 I'm creating a higher-order component to wrap a component extending the interface: interface ClickOutsideInterface { onClickOutside: (e: Event) => void } The factory I've created expects a React.ComponentClass implementing the ClickOutsideInterface : function clickOutside<P>(Component: React.ComponentClass<P> & ClickOutsideInterface){ return class ClickOutside extends React.Component<P, {}> { /* on click outside invoke Component.onClickOutside */ render() { return(<div><Component {...this

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

与世无争的帅哥 提交于 2020-01-01 12:17:32
问题 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" />,

Passing props to Higher-Order Component

微笑、不失礼 提交于 2019-12-30 07:10:28
问题 I have a higher-order component FormBuilder like this: const FormBuilder = (WrappedComponent) => { return class HOC extends React.Component { clearForm() { // ... } render() { return ( <Form onSubmit={//what do I say here?!}> <Form.Input placeholder='Name' name='name' /> <WrappedComponent clearForm={this.clearForm} /> <Form> ); } } } And here is the WrappedComponent NewPizzaForm : class WrappedComponent extends React.Component { onSubmit() { // sends a POST request to the backend, then this

HoC for context consumer causing type error with child component

北城余情 提交于 2019-12-24 20:39:39
问题 I am having an issue wherein I attempt to write a HoC in order to wrap components that should have access to a particular context via newly injected props. There is a Gist related to a tutorial which basically contains the following snippet: export function withAppContext< P extends { appContext?: AppContextInterface }, R = Omit<P, 'appContext'> >( Component: React.ComponentClass<P> | React.StatelessComponent<P> ): React.SFC<R> { return function BoundComponent(props: R) { return (

higher order function render twice causing child component to be triggered

北城余情 提交于 2019-12-24 14:09:48
问题 I have a protected route, implemented using higher order component, but in my App.js which is the child component, trigger its componentDidMount method, I wonder why is it so.. Route.js ... <BrowserRouter> <Switch> <Route exact path='/app' component={CheckPermission(App)} /> <Route exact path='/app/login' component={Login} /> </Switch> </BrowserRouter> ... Auth.js export default function CheckPermission(EnhancedComponent) { class Auth extends Component { static contextTypes = { router:

React router v4 - Authorized routes with HOC

耗尽温柔 提交于 2019-12-18 07:14:37
问题 I have a problem to prevent unauthorized users from accessing authorized-only routes/components - such as logged in users dashboard I have the following code: import React from 'react' //other imports import {withRouter} from 'react-router' class User extends React.Component { constructor(props) { super(props) console.log('props', props) let user = JSON.parse(localStorage.getItem('userDetails')) if(!user || !user.user || props.match.params.steamId !== user.user.steamId) { props.history.push('