【发布时间】:2019-02-14 15:15:01
【问题描述】:
我的 React-Native 项目中的 unstable_Profiler 存在一个问题,它忽略了 onRender 回调,但仅限于生产模式。没有错误,一切都很好。我通过这篇文章去了:https://itnext.io/react-native-profiler-43d131130c5c
我在开发模式(react-native run-android)下测试了解决方案,一切都很完美。应用程序的生产版本不起作用。 我尝试了最新版本的 react 和 react-native、react-dom、schedule、scheduler、modify .babelrc,但没有任何效果。
import React, { unstable_Profiler as Profiler } from 'react';
const withProfiler = (profilerId) => (WrappedComponent) => {
class ProfilerComponent extends React.Component {
async logMeasurement(id, phase, actualDuration, baseDuration) {
// see output during DEV
console.log({id, phase, actualDuration, baseDuration});
// also here is some logic to log durations in prod mode. (eg. logcat)
// but it never happened.
}
render() {
return (
<Profiler id={profilerId} onRender={this.logMeasurement}>
<WrappedComponent {...this.props} />
</Profiler>
);
}
}
return ProfilerComponent;
};
export default withProfiler;
.babelrc
{
"presets": [
"module:metro-react-native-babel-preset"
],
"plugins": [
["module-resolver", {
"root": ["./"],
"alias": {
"react-dom$": "react-dom/profiling",
"scheduler/tracing": "scheduler/tracing-profiling"
}
}]
],
"env": {
"production": {
"plugins": [
"transform-remove-console",
]
},
"development": {
"plugins": [
"@babel/plugin-transform-react-jsx-source"
]
}
}
}
package.json
"react": "^16.8.1",
"react-native": "^0.57.8",
"react-dom": "16.8.1",
"react-art": "16.8.1",
"schedule": "^0.4.0",
"scheduler": "^0.13.1",
"@babel/core": "7.1.0",
"@babel/plugin-proposal-decorators": "^7.3.0",
"@babel/plugin-transform-react-jsx-source": "^7.2.0",
"@babel/preset-env": "^7.3.1",
"@babel/register": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^8.0.4",
"babel-plugin-module-resolver": "^3.1.3",
"babel-plugin-transform-remove-console": "^6.9.4",
"metro-react-native-babel-preset": "^0.48.1",
预期结果是logMeasurement 方法正在生产应用程序中运行。
编辑
我的logMeasurement 绑定无效。这是我修复它的方法。
logMeasurement = async (id, phase, actualDuration, baseDuration) => { ... }
但是,它并没有解决问题。回调仍未被调用。
【问题讨论】:
-
你不是binding
this.logMeasurement,这有关系吗? -
感谢@Jacob 的回复。我已经在上面的代码中修复了它,但没有解决这个问题。我已经编辑了这篇文章。
-
嗨,@user 6547076。您找到解决方案了吗?我被困在同一个问题上。在生产模式下不起作用。
标签: javascript android reactjs react-native