3 回答
TA贡献1898条经验 获得超8个赞
你可以利用三元条件
<Input
type={type}
name={input_key}
id={input_key}
{...(input_key === 'number'? min_max: {})}
required={required}
placeholder={placeholder}
/>
TA贡献1752条经验 获得超4个赞
只需有一个条件并使用传播语法。
// sample prop
let input_props = {
type,
name: input_key,
id: input_key,
}
// condition to add min_max as prop.
if (input_key === 'number') {
input_props = {
...input_props,
...min_max // include min_max
}
}
return (
<Input
{...input_props} // spread syntax to pass all input_props
required={required}
placeholder={placeholder}
/>
)
TA贡献1812条经验 获得超5个赞
没什么特别的
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
const min_max = {
max : 10,
min : 1
}
const j = false;
return (
<div>
<input type="number" { ...(j && min_max || {})} />
</div>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
添加回答
举报