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

React Native: How can I export a state from one component to another?

$
0
0

So I have this:

class Selection extends React.Component {state = {    role1: true,    role2: false}...

Which I want to export to file2 which looks like:

class SignUpAdrress extends React.Component {handlePress = () => {        if(role1){ // <- Here's where I want it            this.props.navigation.replace('SignupPayment')        }else{            this.props.navigation.replace('Login')        }};...

How can I do this if these components are not father-child? I've tried using React context which went somehting among the lines:

file1

export let isRole1 = React.createContext({   Role1: true,   Role2: false})class Selection extends React.Component {state = {    Role1: true,    Role2: false}

File 2

import {isRole1} from './File1';class SignUpAdrress extends React.Component {handlePress = () => {<isRole1.consumer>        if(role1){             this.props.navigation.replace('SignupPayment')        }else{            this.props.navigation.replace('Login')        }</isRole1.consumer>};

Viewing all articles
Browse latest Browse all 6287

Trending Articles



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