2

My components looks like:

App.jsx

import MyInput from './MyInput';

const onChangeHandler = (val) => {
    console.log(val);
};

export default {
    render() {
        return (
            <MyInput onChange={onChangeHandler} />
        );
    },
};

and MyInput.jsx

export default {
    props: {
        onChange: {
            type: Function,
        },
    },
    render() {
        // as Sphinx suggested it should be this.$props.onChange
        return (
            <input onChange={this.$props.onChange} />
        );
    },
};

But this.onChange is undefined:

enter image description here

How to properly use this.onChange prop in MyInput component?

CodePen

Here you can find CodePen with implementation of my problem: https://codepan.net/gist/13621e2b36ca077f9be7dd899e66c056

6
  • onChange is nested, wouldn't it be this.props.onChange? Admittedly it's been a while since I've worked with Vue. Commented Jun 7, 2018 at 21:10
  • @Ben I've tried it - the same results: this.props is undefinded Commented Jun 7, 2018 at 21:27
  • it should be _this.$props Commented Jun 7, 2018 at 21:29
  • To be clear: this.$props.onChange, this.props.onChange, _this.$props.onChange and this.onChange all of them failed. Commented Jun 7, 2018 at 21:32
  • what is the error for _this.$props? I created one demo below, it works fine. did you pass correct value like <my-input :on-change="function(){}"></my-input>? Commented Jun 7, 2018 at 21:39

2 Answers 2

1

Don't start your prop name with on. The 'on' prefix in reserved.

Credits to: nickmessing - see his answer

Sign up to request clarification or add additional context in comments.

Comments

0

Check Vue API: instance property=$props, you should use _this.$props like below demo:

Vue.config.productionTip = false

Vue.component('child', {
  props: {
    onChange: {
        type: Function,
        default: function () {console.log('default')}
    },
  },
  render: function (h) {
    let self = this
    return h('input', {
      on: {
        change: function (e) {
          var test;
          (test = self.$props).onChange(e)
        }
      }
    })
  }
})

Vue.component('container1', {
  render: function (h) {
    return h('child', {
      props: {
        onChange: this.printSome
      }
    })
  },
  methods: {
    printSome: function () {
      console.log('container 1 custom')
    }
  }
})

Vue.component('container2', {
  render: function (h) {
    return h('child', {
      props: {
        onChange: this.printSome
      }
    })
  },
  methods: {
    printSome: function () {
      console.log('container 2 custom')
    }
  }
})

new Vue({
  el: '#app'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
  <h3>Container 1</h3>
  <container1></container1>
  <h3>Container 2</h3>
  <container2></container2>
</div>

3 Comments

this.$props.onChange looks great. But the problem is that this.$props.onChange is still undefined. Can you update your answer to more accurately describe my example of usage MyInput in App? I can only use render functions - HTML from your last snippet is not applicable in my example.
@Everettss updated. I created two components=**container1/2** using render() function, then they will pass own custom function to props =onChange
I've tried to rewrite your example in JSX and without success. You can check this codepen with your example rewritten to JSX codepan.net/gist/0538859e1768bad79035703c4b588d4e

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.