简体   繁体   English

如何有条件地在反应组件上添加道具?

[英]how to conditionally add props on a react component?

I want to conditionally add props on an input type, I want to check if input_key is a number and if it is, I want to add min_max props, cause I don't want it added if it's of any type我想有条件地在输入类型上添加道具,我想检查input_key是否是数字,如果是,我想添加min_max道具,因为如果它是任何类型,我不希望添加它

 const min_max = {
   min: 0,
   max: 100
 };
 <Input
    type={type}
    name={input_key}
    id={input_key}
    {input_key === 'number' && ...min_max}
    required={required}
    placeholder={placeholder}
  />

how do I make it work by using spread like that?我如何通过使用这样的传播使其工作?

You can make use of ternary condition您可以利用三元条件

<Input
    type={type}
    name={input_key}
    id={input_key}
    {...(input_key === 'number'? min_max: {})}
    required={required}
    placeholder={placeholder}
  />

Simply have a condition and use spread syntax.只需有一个条件并使用扩展语法。

// 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}
  />
)

Nothing special没什么特别的

 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>

I think you can do this if you have min and max prop to your input as我认为如果你的输入有 min 和 max 属性,你可以这样做

 <Input
type={type}
name={input_key}
id={input_key}
max={inputKey === 'number' ? min_max.max : ''} 
min={inputKey === 'number' ? min_max.min : ''} 
required={required}
placeholder={placeholder}
/>

Hope it helps希望能帮助到你

You can make it into a function你可以把它做成 function

  const getNumberInputProps = (inputKey, otherProps) => {
    if (inputKey === "number")
        return otherProps
    return {}
}

then you can call it and spread the output like this然后你可以调用它并像这样传播 output

 <Input
  type={type}
  name={input_key}
  id={input_key}
  {...getNumberInputProps(input_key, min_max)}
  required={required}
  placeholder={placeholder}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有条件地将一组道具添加到反应组件? - How to conditionally add a group of props to a react component? 如何有条件地将道具传递给 React JS 中的组件 - How to pass props conditionally to a component in React JS React:如何在库中的组件上添加道具? - React: how to add a props to a component inside a library? TypeScript:有条件地在React组件中扩展道具 - TypeScript: Extend props in React component conditionally 当我们使用 React 和 Typescript 时,如何有条件地将可选道具传递给组件? - How to conditionally pass optional props to component when we use React with Typescript? 如何使用React Native中的导航器动态地向组件添加新道具 - How to add new props dynamically to the component using navigator in React Native 如何在 React 功能组件中添加 Redux function 作为带有 Props 的参数 - How to Add Redux function as a Parameter with Props in React Functional Component React Styled组件:如何基于道具添加CSS样式? - React Styled component: how to add css styles based on props? 如何在 React 功能组件中使用 Props 添加 Redux object 作为参数 - How to Add Redux object as a Parameter with Props in React Functional Component javascript object reduce中如何有条件地添加props? - How to conditionally add props in javascript object reduce?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM