简体   繁体   English

反应:当我在表单中使用 input type="file' 时,我应该在 propTypes 中写什么?

[英]REACT: What I should write in propTypes when in form I use input type="file'?

I make form using Formik in my app.我在我的应用程序中使用Formik制作表格。 When I send form to my local server I create image with title.当我将表单发送到本地服务器时,我会创建带有标题的图像。 Attach images I should using input type="file" .But I have very little experience using the formik.附加图像我应该使用input type="file" 。但是我使用 formik 的经验很少。

What I should write in propTypes when in form I use input type="file' in file InputImage.js?当我在文件 InputImage.js 中使用 input type="file' 时,我应该在 propTypes 中写什么?

And How to add input type="file" in file AddImage.js in mark place?以及如何在标记位置的文件 AddImage.js 中添加input type="file"

Now I want to create input which attach image component InputImage.js similar to InputTitle.js.现在我想创建类似于 InputTitle.js 的附加图像组件 InputImage.js 的输入。

I comment line where I dont know what I should write.我在不知道应该写什么的地方发表评论。

AddImage.js: AddImage.js:

const AddImage = (props) => {
    
    const {handleSubmit, values, handleChange} = useFormik({  
      initialValues: {
          title: '',
          image: ''             // Did I write correctly here?
      },
        validateOnchange: false,
        onSubmit: async (formValues) => {
              const response = await api(`${imageRoutePath}`, {
                  method:'POST',
                  body: JSON.stringify(formValues),
              });},  
    });
    
   return (
    <div>
      <form onSubmit={handleSubmit}>   
       <InputTitle                             
         label="title"
         id="title" 
         inputProps={{
           name:'title',
           value: values.title,
           onChange: handleChange,
       }}
       />
       
       <InputImage                             
         label="image"
         id="image" 
         inputProps={{
           name:'image',

            // WHAT I SHOULD WRITE THERE?  

           onChange: handleChange,
       }}
       />
       
          <button type="submit" disabled={isSubmitting}>Add</button>
     </form>
   </div>
   );
};

export default AddImage;

InputImage.js:输入图像.js:

const InputImage = ({
    label, inputProps, error, id,     
}) => (
        <div className="formInputCategory">
          <label htmlFor={id} className="formInputLabelCategory">  
            {label}
          </label>
        <input {...inputProps} id={id} />    
          {error && <span className="formInputErrorCategory">{error}</span>} 
        </div>
    );

InputImage.propTypes = {                   
    label: PropTypes.string.isRequired,
  
     // WHAT I SHOULD WRITE THERE?

    error: PropTypes.string,
    id: PropTypes.string.isRequired, 
};

InputImage.defaultProps = { 
    error: '',
}

--------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------

example how I write InputTitle.js:例如我如何编写 InputTitle.js:

const InputTitle = ({
    label, inputProps, error, id,     
}) => (
        <div className="formInputCategory">
          <label htmlFor={id} className="formInputLabelCategory">  
            {label}
          </label>
        <input {...inputProps} id={id} />    
          {error && <span className="formInputErrorCategory">{error}</span>} 
        </div>
    );


InputTitle.propTypes = {                   
    label: PropTypes.string.isRequired,  
    inputProps: PropTypes.instanceOf(Object).isRequired,  
    error: PropTypes.string,
    id: PropTypes.string.isRequired, 
};

InputTitle.defaultProps = { 
    error: '',
}

Formik doesnot support fileupload by default, But you can try the following Formik 默认不支持文件上传,但是你可以试试下面的

<input id="file" name="file" type="file" onChange={(event) => {
  setFieldValue("file", event.currentTarget.files[0]);
}} />

Here "file" represents the key that you are using for holding the file这里的“文件”代表您用于保存文件的密钥

setFieldValue is obtained from <Formik /> setFieldValue 是从<Formik />中获取的

reference: formik setFieldValue prop参考: formik setFieldValue 道具


your code will look like:您的代码将如下所示:


const AddImage = (props) => {

    const {handleSubmit, values, handleChange, setFieldValue } = useFormik({  
      initialValues: {
          title: '',
          image: ''             // Did I write correctly here?
      },
        validateOnchange: false,
        onSubmit: async (formValues) => {
              const response = await api(`${imageRoutePath}`, {
                  method:'POST',
                  body: JSON.stringify(formValues),
              });},  
    });

   return (
    <div>
      <form onSubmit={handleSubmit}>   
       <InputTitle                             
         label="title"
         id="title" 
         inputProps={{
           name:'title',
           value: values.title,
           onChange: handleChange,
       }}
       />

       <InputImage                             
         label="image"
         id="image" 
         inputProps={{
           name:'file',
            id="file",
            // WHAT I SHOULD WRITE THERE?  
            type="file",
            onChange={(event) => {
              setFieldValue("file", event.currentTarget.files[0]);
            }},
       }}
       />

          <button type="submit" disabled={isSubmitting}>Add</button>
     </form>
   </div>
   );
};

export default AddImage;

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

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