I'm stuck with a react native problem : I've my component with a (supposed) valid constructor, but I can't access to this.props
In my code :
//In another file
interface mainFunction {
loaded: () => void,
disconnect: () => void
}
interface disconnect {
mainExport:mainFunction
}
class Disconnect extends Component<disconnect> {
constructor(props:any) {
super(props);
console.log(props)
}
test() {
console.log("Disconnect...");
console.log(this.props);
}
render() {
return (
//Some react render
);
}
export default Disconnect;
When I console log props in my constructor, there are functions that I give to this component, but in my test function this.props is undefined... I try to set this.props = props in the contructor by props is a readOnly propertie (I use TypeScript) What did I do wrong ?
EDIT____ I try to : create a new attribute to store my function :
class Disconnect extends Component<disconnect> {
disconnect:any
constructor(props:any) {
super(props);
console.log(this.props.mainExport.disconnect)
this.disconnect = this.props.mainExport.disconnect
}
}
But this.disconnect is unavariable in my other function
I try to bind my test function in the constructor but it doesn't work too ...
ANSWER_____ Okay so @Chris got the answer : I need to convert my test method to a variable :
//Old
test() {
console.log("Disconnect...");
console.log(this.disconnect_func);
console.log(this.disconnect);
}
//New
test = () => {
console.log("Disconnect...");
console.log(this.disconnect_func);
console.log(this.disconnect);
}
and it works