1

My goals are to make a redirection with a BACK button, but until now no success. When i'm trying to pass a function from child to parent it return me :

_this.props.callHomeFunction is not a function

Parent.js

  constructor(props) {
        super(props);
        this.state = {
            okRedirect: false,
        }

        this.consoleToHome = this.consoleToHome.bind(this)
    }

    consoleToHome = (test) => {
        // this.setState({ okRedirect: true });
        console.log('go to home');
        console.log(test);
    }
render(){
  return(
    <Router>
      <Select callHomeFunction={this.consoleToHome} />

Child.js

class BackButton extends Component {

  changePage = () => {
    const test = 'test';
    console.log('click on home');
    console.log(this.props);
    this.props.callHomeFunction(test);
  }
render() {
    return (
      <div className='back_buttons'>
        <ul>
          <li>
            <button className='back_buttons back_button'
              onClick={this.changePage}
            >
7
  • Child cannot send props to parent. Commented Feb 19, 2019 at 10:37
  • 1
    You don't have to bind consoleToHome on this as you are using an arrow function. (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) Commented Feb 19, 2019 at 10:38
  • 3
    You're passing a function to Select, not BackButton Commented Feb 19, 2019 at 10:41
  • @estus if i pass onClick={this.changePage()} the result is the same , but this time doesnt show the next page after the Home Commented Feb 19, 2019 at 11:48
  • it should be onClick={this.changePage}, not onClick={this.changePage()}. If the problem persists, consider updating the question with stackoverflow.com/help/mcve that reflects your current attempt. Commented Feb 19, 2019 at 11:51

2 Answers 2

1

Your binding's of the components are correct as far I see the code,

Please check the Component names, You child Component name is BackButton but when you are including you are using <select>.

If the above didn't solve the problem please post your error

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

1 Comment

yep i found my mistake . Cheers mate !
0

Use constructor with super function in Child.js

constructor(props) {
    super(props);
}

Hope it will work

3 Comments

nope , i had a constructor before , but the result is the same
lets try to add bind method in onclick method like this onClick={this.changePage.bind()} and also add constructor with super constructor(props) { super(props); }
Unfortunately the result stays the same

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.