I want to show a custom input component and then call its method on a button click:
const Parent = () => {
const customInputRef = useRef(null);
const [customInputVisible, setCustomInputVisible] = useState(false);
async function onPress() {
setCustomInputVisible(true);
await resolvePendingChanged(); // customInput is not null and can be accessed
customInputRef.current.customMethod();
}
return (
<View>
<Button onPress={onPress}>Press me!</Button>
{customInputVisible && <CustomInput ref={customInputRef} />}
</View>
);
}
I saw that people use a custom forceUpdate function in order to trigger a component update but that didn't really help in my case.
In Svelte there's this "tick" lifecycle hook that does exactly what I need.
It returns a promise that resolves as soon as any pending state changes have been applied to the DOM (or immediately, if there are no pending state changes).
Is there an equivalent of Svelte's tick
in React and if not how can I solve this problem in React?