How to pass a value from one class to the main one in React

Viewed 30

Lets say I have these two files: App.js (the file that actually runs the program)

const App = () => {
    const [variable1, setVariable1] = useState(0);
    return (
    <NewFile />
    );
}
export default App;

NewFile.js

const NewFile = () => {
    const [partText, setPartText] = useState("");
    const [wholeText, setWholeText] = useState("");
    if (partText != "")
    {
       setWholeText(partText);
    }
 return (
    {wholeText}
    );
}
export default NewFile;

Now, here comes my question. How can I set "variable1" to be equal to "wholeText"? The most logical solution would be: setVariable1(NewFile.wholeText) and obviously it doesn't work like that. I have seen some solutions with props but from what I understood I can only pass the value from (in this case) App.js to NewFile.js because I am calling NewFile.js in App.js.

I might have understood props wrongly and there might be a simple way to do the opposite, but I really can't think of anything else for now.

0 Answers
Related