猴子修补 formy-react 库中使用的函数



我使用 formy-react v1.1.5 进行验证,我有大约 100 个输入字段,由于代码中不必要的 object.assign 函数,它的速度慢得令人难以置信。我知道更高版本解决了这个问题,但我现在无法更新它。

我对猴子补丁一无所知,我不想使用任何补丁库来完成工作。我想了解如何修补它。

此代码:

getCurrentValues = () => (
this.inputs.reduce((data, component) => {
const { name } = component.props;
const dataCopy = Object.assign({}, data); // avoid param reassignment
dataCopy[name] = component.state.value;
return dataCopy;
}, {})
)
getPristineValues = () => (
this.inputs.reduce((data, component) => {
const { name } = component.props;
const dataCopy = Object.assign({}, data); // avoid param reassignment
dataCopy[name] = component.props.value;
return dataCopy;
}, {})
)

我想进行以下更改:

getCurrentValues = () => (
this.inputs.reduce((data, component) => {
const { name } = component.props;
data[name] = component.state.value;
return data;
}, {})
)
getPristineValues = () => (
this.inputs.reduce((data, component) => {
const { name } = component.props;
data[name] = component.props.value;
return data;
}, {})
)

谢谢。

最简单快捷的方法:

转到"node_modules/formy-react/lib",打开要修补的文件,更改并保存。

下次执行它时,它将使用更改的文件。

唯一的问题是,每次在每台计算机上进行 npm 安装时,都需要再次执行此操作。

更好的方法是创建一个文件.js,并在 package.json 中创建一个脚本,将其复制到正确的位置,并在自述文件中添加提醒,说明您需要在执行 npm 安装后执行脚本。

可能是这样的:

"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node index.js",
"patch": "mv patchedFile.js ./node_modules/formsy-react/lib/{fileName}.js"
},

正在修补文件.js在项目根目录中,并且具有与 {文件名}.js相同的内容并进行修改。

只需在"npm install"之后执行"npm 运行补丁"。

最新更新