Skip to content

Instantly share code, notes, and snippets.

@iamtmrobinson
Created January 8, 2018 14:49
Show Gist options
  • Select an option

  • Save iamtmrobinson/d4bb6e9297300b787891337fe9e07c42 to your computer and use it in GitHub Desktop.

Select an option

Save iamtmrobinson/d4bb6e9297300b787891337fe9e07c42 to your computer and use it in GitHub Desktop.
Using custom props with a Redux form in Typescript
import * as React from 'react';
import {
Field as FormField,
InjectedFormProps,
reduxForm,
} from 'redux-form';
interface CustomProps {
customText: string;
}
class FormComponent extends React.Component<CustomProps & InjectedFormProps<{}, CustomProps>> {
render() {
const { handleSubmit, customText } = this.props;
return (
<form onSubmit={handleSubmit}>
<div>
<p>{customText}</p>
</div>
</form>
);
}
}
export const Form = reduxForm<{}, CustomProps>({
form: 'form',
})(FormComponent);
@ootes
Copy link
Copy Markdown

ootes commented May 25, 2018

Many thanks!

@mcroba
Copy link
Copy Markdown

mcroba commented Jul 3, 2018

Thank you!

@lexan1982
Copy link
Copy Markdown

At last i will not use "as any". Thank you.

@jkusachi
Copy link
Copy Markdown

Thank you! this saved me!

@rmaury
Copy link
Copy Markdown

rmaury commented Oct 8, 2018

YES ! Many thanks!

@naveedpash
Copy link
Copy Markdown

Love

@david-cho-lerat-HL2
Copy link
Copy Markdown

🎉

@hitomim
Copy link
Copy Markdown

hitomim commented Dec 12, 2018

Thank you!

@lmiller1990
Copy link
Copy Markdown

excellent!

@duynguye
Copy link
Copy Markdown

Oh man this is a life saver! Thank you!

@hmatthews26
Copy link
Copy Markdown

Thank you so much!

@matti-kone
Copy link
Copy Markdown

awesome!

@billyzduke
Copy link
Copy Markdown

Yes, thanks tremendously!!! But could someone please explain (or point me toward an external explanation of) WHY this works (and/or why this is the ONLY way it works out of the numerous ones I've been trying)?

I am trying to wrap my head around <CustomProps & InjectedFormProps<{}, CustomProps>> and utterly failing...

@heiwais25
Copy link
Copy Markdown

Thank you!!!

@DmytroStepanenkoIdelic
Copy link
Copy Markdown

thank you

@sajadghawami
Copy link
Copy Markdown

thanks mate! :)

@ibarapascal
Copy link
Copy Markdown

ibarapascal commented Sep 30, 2019

Thanks a lot!!!

Keyword: InjectedFormProps

Here is another same solution using InjectedFormProps with withStyles in typescript

import {
  Field,
  reduxForm,
} from 'redux-form/immutable';
import { InjectedFormProps } from 'redux-form';

const styles = (theme: Theme) => createStyles({
})

interface Props extends InjectedFormProps, WithStyles<typeof styles> {
}

interface State {
}

class YourComponent extends React.Component<Props, State>{
}

export default reduxForm({
})(withStyles(styles)(YourComponent));

@Jelle-vz
Copy link
Copy Markdown

Lifesaver!

@VladislavRUS
Copy link
Copy Markdown

Thanks a lot! Saved my day )

@paulanelo
Copy link
Copy Markdown

Thank you!!

@sgltkn
Copy link
Copy Markdown

sgltkn commented Apr 21, 2020

That's awesome. Thanks. :)

@hejiaji
Copy link
Copy Markdown

hejiaji commented May 8, 2020

Thanks, dude. this should be added into the official document.

@Narven
Copy link
Copy Markdown

Narven commented Jun 9, 2020

that help me remove another // @ts-ignore
thanks

@kbaklaev
Copy link
Copy Markdown

dude you're a genius!

@Honka3rd
Copy link
Copy Markdown

stared!

@Goran7777
Copy link
Copy Markdown

Goran7777 commented Aug 8, 2021

Thank you buddy,preety hard task.One more star (Y).Cheers.

@ChristopherHButler
Copy link
Copy Markdown

Has anyone converted this to a functional component?

@myCatPear
Copy link
Copy Markdown

omg, thx!!

@qTheSky
Copy link
Copy Markdown

qTheSky commented Oct 4, 2022

thank you very much

@richcarrot
Copy link
Copy Markdown

This is still paying dividends! Helped me today, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment