Search code examples
javascriptreactjsexportglobal-variablesstate

Is there an any way to pass data through export functions?


export default function Example() { 
    const [name, setName] = useState(""); 
    return (<div>Example</div>);
}

I want to export {name}, because I will use it on another component. I just can do it with a global state (Context API or Redux), or there is a way to export var {name} which is rendered inside another export, easily?


Solution

  • If you want to use name outside of an Example component tree (as a parent Component or somewhere else entirely), you'll need to either lift that state up to a higher common component, or use Context or Redux like you noted.