Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

How to access a function in a stateless component?

$
0
0

Can I access a function in a stateless component? I know that the function can be accessable if change to class component. However, as I know. Hooks is incomptable with class component. Please feel fee to comment. Thank you.

# This example is incorrect, for describe the idea only.
const StatelessComponent = () => {

  const StatelessComponentFunction = () => {
    return 1;
  }

}

const thisFun = new TestComponent();

let A = thisFun.StatelessComponentFunction();

Update on 16, October 2019, solution found.

Base on the information on these pages "https://hackernoon.com/javascript-state-encapsulation-without-classes-in-2019-97e06c6a9643" and "https://basarat.gitbooks.io/typescript/docs/javascript/closure.html". I found a solution.

Modified

const StatelessComponent = () => {

  return {
    StatelessComponentFunction() {
      return 1;
    }
  }

}

const thisFun = StatelessComponent();

console.log(thisFun.StatelessComponentFunction());

Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>