function SentenceBuildA()
{
let pvalue="31/12/2024";
}
export default SentenceBuildA;
Need to another componnet
let childval=pvalue;
function SentenceBuildA()
{
let pvalue="31/12/2024";
}
export default SentenceBuildA;
Need to another componnet
let childval=pvalue;
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 31, 2024, 7:31 AM EST
Hi amar Praveen,
import React from 'react'; function SentenceBuildA() { let pvalue = "31/12/2024"; return pvalue; } export default SentenceBuildA; function AnotherComponent() { const childval = SentenceBuildA(); return ( The value from SentenceBuildA is: {childval} ); }Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 1, 2025, 11:55 PM EST
To pass the value from one component to another in React, you can use props. Here's how you can modify your code:
Define the value in the parent component and pass it as a prop to the child component.
Access the prop in the child component.
Here's an example:
Parent Component (e.g.,
ParentComponent.js)import React from 'react'; import SentenceBuildA from './SentenceBuildA'; function ParentComponent() { const pvalue = "31/12/2024"; return ( ); } export default ParentComponent;Child Component (e.g.,
SentenceBuildA.js)import React from 'react'; function SentenceBuildA({ pvalue }) { return ( The value is: {pvalue} ); } export default SentenceBuildA;In this example, the
ParentComponentpasses thepvalueto theSentenceBuildAcomponent as a prop. TheSentenceBuildAcomponent then receives this prop and displays it.Thanks