reactjs – 没有为外部(npm打包)组件调用React setState回调
我正在使用带有回调的setState来确保在状态更新后运行.
... this.setState({enabled : true},this.getData); } getData () { const self = this; fetch(this.props.url,{ method : 'post',body : this.state }).then(function (response) { return response.json(); }).then(function (result) { self.update(); }); } ... setState被调用了. this.state.enabled确实变为true.但是,没有调用this.getData. 我发现有趣的一件事是,我正在通过npm包使用的组件正在发生这种情况.在我自己的代码中,带回调的setState按设计工作. 所述外部组件也由我包装.我正在使用webpack来构建它.我的webpack配置可能有问题吗? 这里是: const Webpack = require('webpack'); const path = require('path'); module.exports = { entry: { index : './src/index.js' },output: { path: path.join(__dirname,'dist'),filename: '[name].js',library : 'TextField',libraryTarget: 'umd' },externals : [ { 'react' : { root : 'React',commonjs2 : 'react',commonjs : 'react',amd : 'react' } } ],module: { loaders: [ { test: /.(js?)$/,exclude: /node_modules/,loader: require.resolve('babel-loader'),query: {cacheDirectory: true,presets: ['es2015','react','stage-2']} } ] },devtool : 'eval' }; 编辑: 所以现在我很确定当我从包中使用我的组件时会发生一些可疑的事情,当我从我的源代码中使用它时. 当我从作为源代码一部分的组件调用setState时,这就是所谓的: ReactComponent.prototype.setState = function (partialState,callback) { !(typeof partialState === 'object' || typeof partialState === 'function' || partialState == null) ? process.env.NODE_ENV !== 'production' ? invariant(false,'setState(...): takes an object of state variables to update or a function which returns an object of state variables.') : _prodInvariant('85') : void 0; this.updater.enqueueSetState(this,partialState); if (callback) { this.updater.enqueueCallback(this,callback,'setState'); } }; 以上内容来自名为ReactBaseClasses.js的文件 当我从打包为npm包的组件调用setState时,这就是所谓的: Component.prototype.setState = function (partialState,callback) { !(typeof partialState === 'object' || typeof partialState === 'function' || partialState == null) ? invariant(false,'setState(...): takes an object of state variables to update or a function which returns an object of state variables.') : void 0; this.updater.enqueueSetState(this,partialState,'setState'); }; 以上内容来自一个名为react.development.js的文件.请注意,回调传递给enqueueSetState.有趣的是,当我进入enqueueSetState时,该函数对回调没有任何作用: enqueueSetState: function (publicInstance,partialState) { if (process.env.NODE_ENV !== 'production') { ReactInstrumentation.debugTool.onSetState(); process.env.NODE_ENV !== 'production' ? warning(partialState != null,'setState(...): You passed an undefined or null state object; ' + 'instead,use forceUpdate().') : void 0; } var internalInstance = getInternalInstanceReadyForUpdate(publicInstance,'setState'); if (!internalInstance) { return; } var queue = internalInstance._pendingStateQueue || (internalInstance._pendingStateQueue = []); queue.push(partialState); enqueueUpdate(internalInstance); }, 解决方法
这可能是React的错误.显然,将enqueueSetState和enqueueCallback功能组合在一起是一个主要的错误修复,但这些是在早期版本的React中单独运行的调用.
https://github.com/facebook/react/issues/8577 导入npm模块时,有时会将不同版本的React作为依赖项引入,使用setState和callbacks创建这些不一致的错误. https://github.com/facebook/react/issues/10320 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |