简体   繁体   English

道具验证中是否缺少?

[英]is missing in props validation?

I'm new to using react and it keeps saying children, type, onClick, buttonStyle, buttonSize is missing in props validation and I am unsure on what to do.我是使用 react 的新手,它一直说 props 验证中缺少 children、type、onClick、buttonStyle、buttonSize,我不确定该怎么做。

Code代码

import React from 'react';
import './Button.css';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
console.log(PropTypes)

const STYLES = ['btn--primary', 'btn--outline'];

const SIZES = ['btn--medium' , 'btn--large'];

export const Button = ({
    children, 
    type, 
    onClick, 
    buttonStyle, 
    buttonSize
}) => {
    const checkButtonsStyle = STYLES.includes(buttonStyle) 
    ? buttonStyle 
    : STYLES [0]
   
    const checkButtonsSize = SIZES.includes(buttonSize) ? buttonSize : SIZES[0]

    return (
        <Link to='/sign-up' className='btn-mobile'>
            <button
            className={`btn ${checkButtonStyle} ${checkButtonsSize}`}
                onClick={onClick}
                type={type}
                >
                {children}
            </button>
        </Link>
    )
}

You are destructuring the props in the export const Button ({here}) =>...您正在解构export const Button ({here}) =>...

This object properties come from the place where you call that component.此对象属性来自您调用该组件的位置。

For example if I use the element: <SignIn onSubmit={this.signIn} onRegister={this.register} />例如,如果我使用元素: <SignIn onSubmit={this.signIn} onRegister={this.register} />

In this case I could use the props {onsubmit, onRegister}在这种情况下,我可以使用道具 {onsubmit, onRegister}

Then I can call its values inside of the component然后我可以在组件内部调用它的值

const SignUp = ({onSubmit, onRegister}) =>{
    return (
        <form id='signin-form' className="bg-grey-lighter flex flex-col sm:w-full h-auto m-auto" onSubmit={onSubmit}>
...
 

So keep in mind that children, type, onClick, buttonStyle and buttonSize have to be declared as props when you call the element Button Just as this:所以请记住,当您调用元素 Button 时,必须将 children、type、onClick、buttonStyle 和 buttonSize 声明为 props 就像这样:

<Button onClick={this.onButtonClick} type={some_variable} ... />

Where this.onButtonClick could be a function and some_variable a local variable. this.onButtonClick 可以是一个函数,而 some_variable 可以是一个局部变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM