React refs with components

前端 未结 2 1255
遇见更好的自我
遇见更好的自我 2020-12-13 10:39

I have React form that has a Component used to render a drop down because the options are coming from an API. However, I can\'t access the ref for the embedded component. I\

2条回答
  •  时光说笑
    2020-12-13 11:17

    Composite components can have their own refs; you can reach in to them to access the refs further down the component hierarchy.

    Example:

    
    
    
    

    However, ssorallen is correct—you should try to avoid this if possible. Instead, you should either pass callbacks into children:

    
    
    
    

    or expose a public API in the child:

    
    
    
    

    or use some other data flow management (e.g. flux, events, etc).

提交回复
热议问题