简体   繁体   中英

React how can i add 'ref' to dom out of render?

the exmcode is here

mines[i] = (
  <p key = {i} ref = {i}></p>
  );
  .....
  render(
      return(
          {mines}
      )
  )

when i render it on my page ,

(actually i added a click function first,the "mines" changed when i click the button)

the browser call error

addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies of React loaded.

how can i do

Cant you put this inside render, but before return? Something like that:

  render(
      mines[i] = (
        <p key = {i} ref = {i}></p>
      );
      return(
        <div className="wrapper">
          {mines}
        </div>
      )
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM