如何在反应中将道具传递给自定义组件-select
How to pass props to custom components in react-select
我正在尝试使用自定义组件作为 react-select 中的输入字段。因为我需要验证,所以我尝试使用 HTML5 oninvalid
(JSX 中的 onInvalid
)作为我的输入标签,并为 oninvalid
设置自定义消息。但是我无法将消息作为道具传递给我在 select 中设置的组件。下面是我的代码。
Input.js
import React from "react";
import { components } from "react-select";
export default class Input extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
console.log("component mounted");
}
setInvalidMessage = event => {
event.target.setCustomValidity("Custom Message");
};
render() {
if (this.props.isHidden) {
return <components.Input {...this.props} />;
}
return (
<components.Input
{...this.props}
required
onInvalid={this.setInvalidMessage}
/>
);
}
}
example.js
import React from "react";
import Input from "./Input";
import Select from "react-select";
import { colourOptions } from "./docs/data";
const InputBoxWithText = props => {
return <Input {...props} />;
};
export default () => (
<form>
<Select
closeMenuOnSelect={true}
components={{ InputBoxWithText }}
options={colourOptions}
/>
<input type="submit" />
</form>
);
如果我在组件属性中传递输入,我会在 Input.js 中获得硬编码消息。如果我通过 InputBoxWithText,我根本看不到输入安装。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import Example from './example';
ReactDOM.render(
<Example />,
document.getElementById('root')
);
谁能告诉我我做错了什么。
我没有解决方案(我也在找同样的东西),但你的例子有多个错误。
要加载输入,您必须编写 components={{ Input: InputBoxWithText }}
,因为 Input
的组件名称不是 InputBoxWithText
。
另外 onInvalid
似乎不是 Input
API 的一部分,所以它永远不会触发。您是否尝试使用 <input oninvalid="" />
..?
不确定您是否已经找到解决方案,但我设法使用箭头函数传递了我的自定义道具
<Select
closeMenuOnSelect={true}
options={colourOptions}
components={{
Input: (inputProps: InputProps) => (
<components.Input {...inputProps} />
),
}}
/>
最好通过select传递自定义道具:
props.selectProps
为避免每次 Select 更新时重新创建自定义组件,这可能会导致意外错误。
在我的例子中,我是这样传递错误的:
<Select
defaultValue={values}
selectProps={{ errors }}
isMulti
options={inventoryList}
onChange={changeTreeElement}
// @ts-ignore
styles={colourStyles}
/>
然后像 colourStyles methods 中的 selectProps.selectProps.errors
一样访问它。
我正在尝试使用自定义组件作为 react-select 中的输入字段。因为我需要验证,所以我尝试使用 HTML5 oninvalid
(JSX 中的 onInvalid
)作为我的输入标签,并为 oninvalid
设置自定义消息。但是我无法将消息作为道具传递给我在 select 中设置的组件。下面是我的代码。
Input.js
import React from "react";
import { components } from "react-select";
export default class Input extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
console.log("component mounted");
}
setInvalidMessage = event => {
event.target.setCustomValidity("Custom Message");
};
render() {
if (this.props.isHidden) {
return <components.Input {...this.props} />;
}
return (
<components.Input
{...this.props}
required
onInvalid={this.setInvalidMessage}
/>
);
}
}
example.js
import React from "react";
import Input from "./Input";
import Select from "react-select";
import { colourOptions } from "./docs/data";
const InputBoxWithText = props => {
return <Input {...props} />;
};
export default () => (
<form>
<Select
closeMenuOnSelect={true}
components={{ InputBoxWithText }}
options={colourOptions}
/>
<input type="submit" />
</form>
);
如果我在组件属性中传递输入,我会在 Input.js 中获得硬编码消息。如果我通过 InputBoxWithText,我根本看不到输入安装。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import Example from './example';
ReactDOM.render(
<Example />,
document.getElementById('root')
);
谁能告诉我我做错了什么。
我没有解决方案(我也在找同样的东西),但你的例子有多个错误。
要加载输入,您必须编写 components={{ Input: InputBoxWithText }}
,因为 Input
的组件名称不是 InputBoxWithText
。
另外 onInvalid
似乎不是 Input
API 的一部分,所以它永远不会触发。您是否尝试使用 <input oninvalid="" />
..?
不确定您是否已经找到解决方案,但我设法使用箭头函数传递了我的自定义道具
<Select
closeMenuOnSelect={true}
options={colourOptions}
components={{
Input: (inputProps: InputProps) => (
<components.Input {...inputProps} />
),
}}
/>
最好通过select传递自定义道具:
props.selectProps
为避免每次 Select 更新时重新创建自定义组件,这可能会导致意外错误。
在我的例子中,我是这样传递错误的:
<Select
defaultValue={values}
selectProps={{ errors }}
isMulti
options={inventoryList}
onChange={changeTreeElement}
// @ts-ignore
styles={colourStyles}
/>
然后像 colourStyles methods 中的 selectProps.selectProps.errors
一样访问它。