ReactJS lifecycle method inside a function Component

Instead of writing my components inside a class, I’d like to use the function syntax. How do I override componentDidMount, componentWillMount inside function components? Is it even possible? const grid = (props) => { console.log(props); let {skuRules} = props; const componentDidMount = () => { if(!props.fetched) { props.fetchRules(); } console.log(‘mount it!’); }; return( <Content title=”Promotions” … Read more

Is using async componentDidMount() good?

Is using componentDidMount() as an async function good practice in React Native or should I avoid it? I need to get some info from AsyncStorage when the component mounts, but the only way I know to make that possible is to make the componentDidMount() function async. async componentDidMount() { let auth = await this.getAuth(); if … Read more

How to tell webpack dev server to serve index.html for any route

React router allows react apps to handle /arbitrary/route. In order this to work, I need my server to send the React app on any matched route. But webpack dev server doesn’t handle arbitrary end points. There is a solution here using additional express server. How to allow for webpack-dev-server to allow entry points from react-router … Read more

Default property value in React component using TypeScript

I can’t figure out how to set default property values for my components using Typescript. This is the source code: class PageState { } export class PageProps { foo: string = “bar”; } export class PageComponent extends React.Component<PageProps, PageState> { public render(): JSX.Element { return ( <span>Hello, world</span> ); } } And when I try … Read more

Set types on useState React Hook with TypeScript

I’m migrating a React with TypeScript project to use hooks features (React v16.7.0-alpha), but I cannot figure out how to set typings of the destructured elements. Here is an example: interface IUser { name: string; } … const [user, setUser] = useState({name: ‘Jon’}); I want to force user variable to be of type IUser. My … Read more