higher-order-components

TypeScript with React Lazy getting promise error

会有一股神秘感。 提交于 2021-02-08 10:57:25
问题 I am using react with typescript.I have used a higher order component to check whether the user is authenticated or not. After adding the hoc i am getting the error in routes as below /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/navigation/routes.ts TypeScript error in /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/navigation/routes.ts(7,36): Type 'Promise<typeof import("/home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/pages

TypeScript with React Lazy getting promise error

青春壹個敷衍的年華 提交于 2021-02-08 10:52:30
问题 I am using react with typescript.I have used a higher order component to check whether the user is authenticated or not. After adding the hoc i am getting the error in routes as below /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/navigation/routes.ts TypeScript error in /home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/navigation/routes.ts(7,36): Type 'Promise<typeof import("/home/nidhin/Documents/Nidhinbackup/F/iot-remsys-demotwo/remsys/src/pages

Passing React context through an HOC to a wrapped component

情到浓时终转凉″ 提交于 2021-02-07 13:58:46
问题 Is there a way that you can pass context through a React higher order component to a the component it wraps? I have a HOC that receives context from its parent and utilizes that context to perform a basic, generalized action and then wraps a child component that also needs to access that same context to perform actions. Examples: HOC: export default function withACoolThing(WrappedComponent) { return class DoACoolThing extends Component { static contextTypes = { actions: PropTypes.object, }

TypeScript and React: How to overload a higher order component / decorator?

折月煮酒 提交于 2021-01-29 08:01:05
问题 I'm building a React app using TypeScript. I'm writing a higher order component and I would like to overload it's arguments. How can I do that? Let me give you what I've tried so that you can understand it better: const myHOC = ((value: string, anotherValue: number) | completeProps: SomeProps) => (WrappedComponent: ComponentClass) => // ... HOC code So it should be either: const myHOC = (value: string, anotherValue: number) => (WrappedComponent: ComponentClass) => // ... or const myHOC =

How can I set the props type in HOC in react-typescript?

自古美人都是妖i 提交于 2021-01-28 20:04:17
问题 I have a simple withAuth HOC. I am trying to define the type for props. const withAuth = (Component: typeof React.Component) => (role: string) => { return (props) => { const { data, loading } = useGetUser(); if (loading) { return <p>Loading...</p>; } if (!data) { return <Redirect ssr to="/api/v1/login" />; } else { if (role && !isAuthorized(data, role)) { return <Redirect ssr to="/api/v1/login" />; } return <Component user={data} loading={loading} {...props} />; } }; }; I tried this: React

Sharing content between adjacent components, HOC?

瘦欲@ 提交于 2021-01-28 19:43:49
问题 I have a container component which renders two components inside it. Depending on some props in the container, the top component can change, but the lower component will always remain the same. I want to render some additional content (DOM elements/other react components) inside the lower component, but the additional content is created inside the top component. I'm struggling to figure out what I need to refactor here to make this work. I initially tried passing the created content up to the

Get wrapped component class from connected HOC in react-redux

懵懂的女人 提交于 2021-01-28 06:58:06
问题 When using the connect function in react-redux , a Higher Order Component is created. From an instance of that HOC, one can access the wrapped instance of the extended component. Is it possible, however, to retrieve the wrapped class from the HOC class? E.g.: class A extends React.Component {} ConnectedA = connect(mapStateToProps, mapDispatch)(A); ConnectedA.some_static_method_to_get_the_wrapped_class(); // === A EDIT: For the sake of clarity, I do not have an instance of ConnectedA available

Redux Connect w/ HOC - TypeError: Cannot set property 'props' of undefined

拜拜、爱过 提交于 2021-01-28 02:18:00
问题 I'm building a quick authentication higher order component in Next.js and am getting some problems with the following code: import SignIn from "../components/sign-in"; import { connect } from "react-redux"; import { useRouter } from "next/router"; const AuthenticationCheck = WrappedComponent => { const { isAuthenticated, ...rest } = props; const router = useRouter(); const protectedPages = ["/colours", "/components"]; const pageProtected = protectedPages.includes(router.pathname); return

Understanding React Higher-Order Components

浪尽此生 提交于 2020-11-30 12:37:21
问题 Can someone please explain Higher-order components in React. I have read and re-read the documentation but cannot seem to get a better understanding. According to the documentation, HOCs help remove duplication by creating a primary function that returns a react component, by passing arguments to that function. I have a few questions on that. If HOCs create a new enhanced component, can it be possible not to pass in any component as argument at all? In an example such as this, which is the

Understanding React Higher-Order Components

谁都会走 提交于 2020-11-30 12:37:10
问题 Can someone please explain Higher-order components in React. I have read and re-read the documentation but cannot seem to get a better understanding. According to the documentation, HOCs help remove duplication by creating a primary function that returns a react component, by passing arguments to that function. I have a few questions on that. If HOCs create a new enhanced component, can it be possible not to pass in any component as argument at all? In an example such as this, which is the