简体   繁体   English

创建可重用组件的正确方法是什么?

[英]What is the proper way to create reusable components react

Learning react and building a page where there is lots of input fields.学习反应并构建一个有很多输入字段的页面。
I want to create components for different input types so later i can use them instead of creating inputs over and over again.我想为不同的输入类型创建组件,以便以后我可以使用它们而不是一遍又一遍地创建输入。

I've wrote something like this but i repeat many things so i thought there might be a better way to do it.我写过这样的东西,但我重复了很多东西,所以我认为可能有更好的方法来做到这一点。

class BaseInput extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <input
        id={this.props.id}
        name={this.props.name}
        type={this.props.type}
        value={this.props.value}
        onChange={this.props.onChange}
        placeholder={this.props.placeholder}
        required
      />
    );
  }
}

export function PasswordInput(props) {
  return (
    <BaseInput
      id={props.id}
      name={props.name}
      type="password"
      value={props.value}
      onChange={props.onChange}
      placeholder={props.placeholder}
    />
  );
}

export function TextInput(props) {
  return (
    <BaseInput
      id={props.id}
      name={props.name}
      type="text"
      value={props.value}
      onChange={props.onChange}
      placeholder={props.placeholder}
    />
  );
}

You can use spread operator to reduce code.您可以使用扩展运算符来减少代码。

class TextInput extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return <input {...this.props} />;
  }
}

export default TextInput;

This way you even don't need a seperate PasswordInput component.这样您甚至不需要单独的 PasswordInput 组件。

<TextInput id="name" type="text" placeholder="Your Name" />
<TextInput id="password" type="password" />

Let's say your reusable component is named FormInput假设您的可重用组件名为FormInput

The component definition should look like this:组件定义应如下所示:

const FormInput = (props: any) => {
  const [value, setValue] = useState(props.value);
  useEffect(() => {
    setValue(props.value);
  }, [props.value])

  return ( 
    <input
       autoComplete={props.autoComplete}
       name={props.name}
       type={props.type}
       id={props.id} 
       value={props.value}
       data-index={props.index} 
       onChange={props.onChange} 
       disabled={props.disabled}
       ariaLabelledBy={props.ariaLabelledBy} 
       required={props.required}/>
  )
};

And in the page where you want to use it you can simply invoke the component pass all the props to the component and implement the onChange event handler on the page:在你想要使用它的页面中,你可以简单地调用组件,将所有的 props 传递给组件并在页面上实现 onChange 事件处理程序:

Let's say you want to invoke the Input component on Login page:假设您要调用登录页面上的输入组件:

const LoginPage = (props: any) => {
    ... // you have some useEffect etc. that is related to the business logic of the page

    const handleChange = (event: any) => {
      // put your logic here
    }

   return (
        ... // other HTML elements/components

       <FormInput type="text" onChange={handleChange} id="test" value="John Doe" ... />
   )
};

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

相关问题 React Components - 创建它们的正确方法是什么? - React Components - What is the proper way to create them? 创建可重用jQuery对象的正确方法 - Proper way to create a reusable jQuery Object 在React中组件之间传递依赖关系的正确方法是什么? - What's the proper way to pass dependencies between components in React? 基于 redux 状态控制反应组件的正确方法是什么 - What is the proper way to control react components based on redux state React JS:可重用组件 - React JS: Reusable components 实现AngularJS控制器中使用的可重用JavaScript对象的正确方法是什么? - What is the proper way to implement a reusable JavaScript object used in AngularJS controllers? 什么是从React中从同一文件导入多个组件的正确方法,而不是导入每个组件 - what is proper way to import multiple components from same file in react instead of getting each component imported 为可重用的 React 组件存储全局变量的最佳方法是什么? - What's the best way to store a global variable for a reusable React component? 反应高阶组件:添加实例方法的正确方法 - React Higher Order Components: Proper way to add instance methods 在两个反应组件之间设置状态的正确方法 - Proper way to set state between two react components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM