Skip to content

Instantly share code, notes, and snippets.

@rohanBagchi
Last active March 21, 2019 06:13
Show Gist options
  • Save rohanBagchi/ed5841d65f12c259b13c780ee308c83e to your computer and use it in GitHub Desktop.
Save rohanBagchi/ed5841d65f12c259b13c780ee308c83e to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
interface ChildrenPropType {
handleChange: (e: any) => void,
value: string
}
interface PropType {
fieldData: any,
shouldReinitialize: (data: any) => Boolean,
getFieldValue: (data: any) => string,
children: (data: ChildrenPropType) => JSX.Element
}
const Inner = (props: PropType) => {
const [inner, setInner] = useState("");
useEffect(() => {
const {
fieldData,
getFieldValue,
shouldReinitialize
} = props;
if (!fieldData) return;
const initialValue = getFieldValue(fieldData) || "";
if (!inner || shouldReinitialize(fieldData)) {
setInner(initialValue);
}
});
const handleChange = (e: any) => setInner(e.target.value);
return props.children({ handleChange, value: inner })
};
export default Inner;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment