I am a bit lost when it comes to passing a state from a component to a function. I have successfully passed a state from Home into ListHome(A component being imported and rendered into Home) but I am struggling to continue passing it to a function that is being rendered in ListHome(Slider). Mainly due to it being a function.
I have looked over the docs again but am still struggling to understand how this should work. A direct edit/example would be super helpful!
I am rendering Home in a stack in App.js. If you wish for me to include this file just let me know.
I appreciate any insight at all more than you know.
Home.js
export default class Home extends React.Component {
constructor(props) {
super(props)
this.state ={
visible: true,
whichComponentToShow: 'Screen1'
};
}
goToMap = () => {
this.setState({whichComponentToShow: 'Screen2'})
}
goToList = () => {
this.setState({whichComponentToShow: 'Screen1'})
}
render(){
if(this.state.whichComponentToShow === 'Screen1'){
return(
<View style={{backgroundColor: '#d1cfcf' ,flex: 1}}>
<ListHome
renderMap = {this.goToMap.bind(this)}
renderList = {this.goToList.bind(this)}
/>
ListHome.js
export default class ListHome extends React.Component {
goToMap = () => {
this.props.renderMap();
}
goToList = () => {
this.props.renderList();
}
render() {
return (
<Slider
renderMap = {this.goToMap.bind(this)}
renderList = {this.goToList.bind(this)}
/>
Slider.js(I have not implemented anything into this file in attempt to pass the state yet)
const Slider = (props) => {
const [active, setActive] = useState(false)
let transformX = useRef(new Animated.Value(0)).current;
//animation code I removed that uses the above const and let
return (
//code I removed that just creates a button.
//The touchable opacity is how I want to use the state.
<TouchableOpacity onPress={() => this.goToMap}>
</TouchableOpacity>
);
}
export default Slider