I am using React Native
for my application, and at one point, I noticed that I have to type this.state.bar[this.state.foo][SOME_NUMBER]
every time, within my components.
This works perfectly fine, but I want to make my code cleaner by calling a function instead. So, I constructed this:
function txt(n){
return this.state.bar[this.state.foo][n];
}
However, when I run this in the Expo
client, I get the following error:
TypeError: undefined is not an object (evaluating 'this.state.bar')
This error is located at:
in App...
....
Here is my entire code.
import React,
{ Component }
from 'react';
import {
...
} from 'react-native';
export default class App extends React.Component {
state = {
foo: 'ABC',
bar: {
'ABC': [
'...',
'...',
'...'
]
}
};
render() {
function txt(n){
return this.state.bar[this.state.foo][n];
}
return (
<View>
...
</View>
);
}
}
I tried placing the text()
function outside the App
class, but got the same error.
When I placed it outside render()
in App
, I got an unexpected token
error.
When I defined this.state
within a constructor(props)
and placed text()
within the constructor
, I got ReferenceError: Can't find variable: text
In order to access the current state in this case, we will need to create the closure around an object. The reference to the object will always be the same in the closure function but the actual value stored by the object will be changed each time the Counter function runs.
This is happening because the getProducts is using the value products had when getProducts was being declared, not the one products has when the getProducts function is called. This is always the case when you want to access the most recent state directly in non-inline functions.
React components can possess internal “state,” a set of key-value pairs which belong to the component. When the state changes, React re-renders the component. Historically, state could only be used in class components. Using hooks, you can apply state to functional components too.
In React we can access the child's state using Refs. we will assign a Refs for the child component in the parent component. then using Refs we can access the child's state.
Your problem is scoping.
The this
that you're trying to access inside the txt()
function is pointing to its own this
, and not the outer component this
.
There are several ways to fix this. here's a few:
You can transform txt
into an arrow function to use the outer this
:
render() {
const txt = (n) => {
return this.state.bar[this.state.foo][n];
}
return (
<View>
...
</View>
);
}
this
render() {
function _txt(n){
return this.state.bar[this.state.foo][n];
}
const txt = _txt.bind(this);
return (
<View>
...
</View>
);
}
this
render() {
const self = this;
function txt(n){
return self.state.bar[self.state.foo][n];
}
return (
<View>
...
</View>
);
}
txt
function to outside of the render function and bind it to the component this
.this
....and I'm sure that there are several other ways to fix this behaviour. You just need to know when you're using the component's this
or some other this
.
A few issues here. First, you need to bind the text
function to the class in the constructor. You also need to move the text
function out of the render method and add it as a class method (no function
in front of function name):
import React,
{ Component }
from 'react';
import {
...
} from 'react-native';
export default class App extends React.Component {
constructor () {
super();
this.state = {
foo: 'ABC',
bar: {
'ABC': [
'...',
'...',
'...'
]
}
};
this.text = this.text.bind(this);
}
text (n) {
return this.state.bar[this.state.foo][n];
}
render() {
return (
<View>
...
</View>
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With