i have this two files Home.js and route.js, in routes.js there's a function defined called componentToRoute, this function returns a JSX component. In the Home.js file this function is imported and called, what i want to do is to keep the this
binding inside the componentToRoute function as well. I tried doing this with Function.prototype.call() and Function.prototype.bind() but it doesn't work.
routes.js
export function componentToRoute(route) {
return (
<View>
<SearchInput
section={route.title}
onInput={this.filterArray(route.key)}></SearchInput>
<ListOfEntries
style={style.list}
onEntryPress={item =>
this.props.navigation.navigate('Local', {
item: item,
})
}
list={this.state[route.key]}></ListOfEntries>
</View>
);
}
Home.js
import {componentToRoute} from './routes';
class Home extends Component {
filterArray = array => text => {
this.setState(previousState => {
return {
[array]: this.fitlerService.filter(
previousState['initial' + array],
'name',
text,
),
};
});
};
renderScene = ({route}) => {
return componentToRoute(route).call(this);
};
}
When the renderScene function is executed it throws this.filterArray is undefined
, however, if i assing the function to an attribute inside the Home class like :
componentRoute = componentToRoute;
and then call this.componentRoute(route)
, the function works correctly (this
is binded to the correct object).
Do you have any idea why neither call() and bind() work correctly in this case?