Component-1
function SentenceBuild(props: {
form: FormHook
fieldPath: EditMedicationForm.InstructionFields;
defaultValue: string;
useDefaultValue: boolean;
readOnly?: boolean;
}) {
const startsOn = moment(instruction.startsOn, ValidDateFormats.NodeDateTimeTimezone);
let showStartsOn = false;
if (startsOn && startsOn.isValid()) {
showStartsOn = moment().endOf('day').isBefore(startsOn, 'day');
return (
{showStartsOn && (
prefix='starting on' readOnly={props.readOnly} value={moment(startsOn).format(ValidDateFormats.Edit)} default='[date]' /> )}
);
}
Compnent-2
const performedOnMoment = Here I want to showStartsOn value
Naveen KumarPosted Dec 28, 2024, 3:00 AM
Using a Callback Function
To share the
showStartsOnvalue fromSentenceBuildtoComponent-2:SentenceBuildthat will send the value ofshowStartsOnup to the parent component.Component-2.1. SentenceBuild (Component-1)
Modify
SentenceBuildto accept a callback function and call it whenshowStartsOnchanges:2. Parent Component
In the parent component, store the
showStartsOnvalue in a state variable and pass the callback function toSentenceBuild. Then, pass the state value toComponent-2.3. Component-2
In
Component-2, use theshowStartsOnvalue as needed:amar PraveenPosted Dec 27, 2024, 10:22 AM
Can you please share the example for above with call back function.How to write code with call back function?
amar PraveenPosted Dec 27, 2024, 8:39 AM
Props willn't work because some other properties also there if I try to put optional there is some other errors are thrown.
Can you please share alternatae sol for this.
Jignesh KumarPosted Dec 27, 2024, 5:19 AM
Hello Praveen,
You can use props to pass data from parent controller to child controler,
Component-1
Component-2
Naveen KumarPosted Dec 27, 2024, 5:06 AM
To pass the
showStartsOnvalue fromComponent-1(SentenceBuild) toComponent-2, you can follow these steps:Approach: Using Props to Pass Data
Expose
showStartsOnas a Prop: ModifyComponent-1(SentenceBuild) to accept a callback function or a state-updating function as a prop to share the value ofshowStartsOn.Lift State Up or Use Parent State: The parent component can act as a bridge to share
showStartsOnbetweenSentenceBuildandComponent-2.Updated
SentenceBuild(Component-1)Parent Component
In the parent component, store the
showStartsOnvalue in a state variable and pass it down toComponent-2.Component-2
Now,
Component-2can directly access theshowStartsOnvalue through its props.Summary of Changes
onShowStartsOnChange) inSentenceBuildto pass the value ofshowStartsOnto the parent.showStartsOnvalue and pass it as a prop toComponent-2.showStartsOnvalue inComponent-2via its props.This approach ensures a clean flow of data between components without tightly coupling them.