I am trying to use this plugin in my project https://github.com/rt2zz/react-native-drawer. I could run the example properly but having problem integrating it.
I am getting error in method openDrawer. "Can not read property drawer of undefined"
I am guessing that I am not defining and using the class in the right way as it should be ( i'm new to javascript OOP ) as in example it use React.createClass({
unlike mine as extending Component and have constructor.
Relevant codes from my class are as follow.
class Search extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
})
};
}
openDrawer(){
this.refs.drawer.open()
}
getInitialState(){
return {
drawerType: 'overlay',
}
}
Render are
render() {
if (this.state.isLoading) {
return this.renderLoadingView();
}
var controlPanel = <MyControlPanel closeDrawer={() => {this.refs.drawer.close()}} />
return (
<View>
<View style={styles.topBar}>
<Drawer
ref="drawer"
>
<MyMainView
/>
</Drawer>
<Text style={styles.topBarMenu}>☰</Text>
<Text style={styles.topBarTitle}>เงินปันผล</Text>
<Text style={styles.topBarRight}></Text>
</View>
If we try to access the current property of the ref directly in the component, we would get undefined back because the ref hasn't been set up and the div element has not been rendered. You can also access the current property of the ref in an event handler function.
You might have already solved this, but just to answer this Q: I had a similar problem and it seems that this happens because the function is being called before Vue has had time to replace the root DOM with its own version. What you can do to fix this is to create a mounted life-cycle hook and call the function there.
Refs are Vue. js instance properties that are used to register or indicate a reference to HTML elements or child elements in the template of your application. If a ref attribute is added to an HTML element in your Vue template, you'll then be able to reference that element or even a child element in your Vue instance.
The key difference is that v-if conditionally renders elements and v-show **conditionally displays **elements. This means that v-if will actually destroy and recreate elements when the conditional is toggled. Meanwhile, v-show will always keep the element in the DOM and will only toggle its display by changing its CSS.
There is a better way than putting this.openDrawer = this.openDrawer.bind(this)
in constructor
Declare openDrawer method with arrows function:
openDrawer = () => {
this.refs.drawer.open()
}
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