1

I am trying to access a method in a nested chilld component using refs. This is to delete data in the nested delete component. My code is as follows (simplified code):

Parent Class:

class Parent extends Component {
  constructor(props) {
    this.childref = React.createRef()
    props.refs(this)
  }
  render() {
    const elements = return [
     <div onclick={this.callsupprimer(0)} />,
     <div onclick={this.callsupprimer(1)} />
    ]
    return (
      <Fragment>
        <Child refs={ref => this.childref = ref>
        </Child>
       loadToolData()
     </Fragment>
     )
  }
  callsupprimer = index => this.childRef.GrandChildRef.supprimer(index)
}
 export withStyles(styles)(Parent)

Child Class:

class Child extends Component {
  constructor(props) {
    this.grandchildref = React.createRef()
    props.refs(this)
  }

  render() {
    return (
      <GrandChild refs={ref => this.grandchildref = ref>
      </GrandChild>
    )
  }
}
 export withStyles(styles)(Child)

GrandChild Class:

class GrandChild extends Component {

  supprimer = (index) => {
        console.log(index)
        this.forceUpdate()
  }
  render() {
    return (
      //blah blah blah
    )
  }
}
export withStyles(styles)(GrandChild)

However, I cannot get the supprimer method to invoke changes in the this context of GrandChild. The method gets called but, in a strange way.

it gets called once when the component is loaded and prints the index, but it does not work onlick!!! I don't even call this method in the grandChild class. Please help.

UPDATE: The code is exactly as written except for the method names now.

11
  • Shouldn't componentDidUPdate(= be componentDidUpdate()? :P Commented Jun 26, 2019 at 12:34
  • ^^ thank you, corrected, but that's not the issue, i just hand wrote this code here, to explain the idea Commented Jun 26, 2019 at 12:39
  • You need to call the ref function in the GrandChild constructor with this props.ref(this), Then you will be able access GrandChild from parent using childref Commented Jun 26, 2019 at 12:39
  • react tells me: TypeError: props.ref is not a function Commented Jun 26, 2019 at 12:44
  • 1
    try to pass this.childref insted of ref => this.childref = ref Commented Jun 26, 2019 at 12:57

1 Answer 1

0

The problem was that I was invoking the callsupprimer when rendering, when it should have been done only onClick, so the method was called once on rendering and it didn't work after. Once I changed the onClick in the parent class to an arrow function callback:

<div onclick={() => this.callsupprimer(1)} />

it worked as expected.

NB: when using withstyles, you need to use refs, and not ref, and then bind the this in the child component as in the question. I didn't need to use current. @Hai Alaluf 's comments in the question pointed me in the right way!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.