最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to properly forward a ref to a material-ui component from a functional component - Stack Overflow

programmeradmin3浏览0评论

I'm using the following packages to create an autoplete solution for an application I'm working on:

  • Material UI 4
  • react-autoplete-input

I'm trying to use the Component prop on the react-autoplete-input element by passing in the material-ui TextareaAutosize ponent.

Directly passing in TextareaAutosize from MUI

import {TextareaAutosize} from '@material-ui/core';
<AutopleteInput Component={TextareaAutosize} />

This works, however I don't have any control over the props it gets.

Through a custom ponent so I can add props

const CustomTextarea = forwardRef((props, ref) => (
  // If I don't forward the ref I get an error...
  <TextareaAutosize
    placeholder="Material-ui through custom ponent..."
    ref={ref}
  />
));

<AutopleteInput Component={CustomTextarea} />

This stops the autoplete from working altogether. However, the placeholder still shows properly which means the props are at least making it through.

You can see all the examples in my sandbox below.

Examples:

I'm using the following packages to create an autoplete solution for an application I'm working on:

  • Material UI 4
  • react-autoplete-input

I'm trying to use the Component prop on the react-autoplete-input element by passing in the material-ui TextareaAutosize ponent.

Directly passing in TextareaAutosize from MUI

import {TextareaAutosize} from '@material-ui/core';
<AutopleteInput Component={TextareaAutosize} />

This works, however I don't have any control over the props it gets.

Through a custom ponent so I can add props

const CustomTextarea = forwardRef((props, ref) => (
  // If I don't forward the ref I get an error...
  <TextareaAutosize
    placeholder="Material-ui through custom ponent..."
    ref={ref}
  />
));

<AutopleteInput Component={CustomTextarea} />

This stops the autoplete from working altogether. However, the placeholder still shows properly which means the props are at least making it through.

You can see all the examples in my sandbox below.

Examples: https://codesandbox.io/s/frosty-wildflower-48iyd

Share edited Mar 26, 2020 at 13:12 jsheffers asked Mar 25, 2020 at 21:16 jsheffersjsheffers 1,6221 gold badge19 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

You just missed to pass the default props

const CustomTextarea = forwardRef((props, ref) => (
  // If I don't forward the ref I get an error...
  <TextareaAutosize
    placeholder="Material-ui through custom ponent..."
    {...props} // here
    ref={ref}
  />
))

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论