Last active
March 21, 2019 06:13
-
-
Save rohanBagchi/ed5841d65f12c259b13c780ee308c83e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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