reactjs - 禁止传播道具 - React Dropzone
问题描述
在其中一个项目中,我为此解决方案使用拖放操作,我选择 React Dropzone 库。该组件工作正常,但在构建项目时出现警告warning Prop spreading is forbidden react / jsx-props-no-spreading
。如何解决错误?
上传文件
const dropzoneRef = createRef();
const openDialog = () => {
if (dropzoneRef.current) {
dropzoneRef.current.open()
}
};
<Dropzone ref={dropzoneRef} noClick noKeyboard>
{({getRootProps, getInputProps, acceptedFiles}) => {
return (
<div className="container">
<div {...getRootProps({className: 'dropzone'})}>
<input {...getInputProps()} /> //Prop spreading is forbidden
<p>Drag 'n' drop some files here</p>
<button
type="button"
onClick={openDialog}
>
Open File Dialog
</button>
</div>
<aside>
<h4>Files</h4>
<ul>
{acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
))}
</ul>
</aside>
</div>
);
}}
</Dropzone>
解决方案
这是一个eslint规则,旨在为作为道具传递的内容创建更多透明度。您最终可以禁用任何 eslint 规则。由您和您的团队决定是否应关闭给定规则。如果是这种情况,您可以转到您的eslint
文件并在rules
include "react/jsx-props-no-spreading": off
。
现在,如果你想加强这个规则,你需要准确地指定传递的道具,而不是像这样传播:
const dropzoneRef = createRef();
const openDialog = () => {
if (dropzoneRef.current) {
dropzoneRef.current.open()
}
};
<Dropzone ref={dropzoneRef} noClick noKeyboard>
{({getRootProps, getInputProps, acceptedFiles}) => {
const divProps = getRootProps({className: 'dropzone'})
const inputProps = getInputProps()
// you could destructure instead, if the keys you extract don't collide.
// if they do collide (like className) and you want to destructure you would need to rename same key names like:
// const { first: inputFirst, second: inputSecond } = getInputProps()
return (
<div className="container">
<div propFirst={ divProps.first } propSecond={ divProps.second } >
<input propFirst={ inputProps.first } propSecond={ inputProps.second } />
<p>Drag 'n' drop some files here</p>
<button
type="button"
onClick={openDialog}
>
Open File Dialog
</button>
</div>
<aside>
<h4>Files</h4>
<ul>
{acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
))}
</ul>
</aside>
</div>
);
}}
</Dropzone>
推荐阅读
- excel - 你好,有没有办法在excel中使用转置函数,不需要固定的单元格范围
- python - pymongo 获取所有文件
- geopandas - 将 GeoPandas 数据框中的几何图形检索为 json [编辑:使用 .to_json()]
- dart - Flutter Launcher 获取系统应用
- python - 在 Python 中使用 GARCH 进行预测
- apostrophe-cms - 撇号 CMS 两列布局不起作用
- python - 根据 Python pandas 中的列值范围添加类别
- c++ - OpenGL glfw cannot draw points
- php - Laravel 5.5 - 从 API 资源类中,我如何获取底层模型类名称?
- java - 无法关闭 HttpClient Wire 调试日志消息