我知道以前有人问过这种问题,但它们与第三方库有关,我的与requireNativeComponent
我React Native从本机iOS组件创建了一个组件
这是我的iOS代码
CanvasView 文件
class CanvasView: UIView {
var lines = [[CGPoint]]()
override init(frame: CGRect) {
super.init(frame:frame)
backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.setStrokeColor(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1))
context.setLineWidth(10)
context.setLineCap(.butt)
lines.forEach { (line) in
for(i,p) in line.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
}
context.strokePath()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lines.append([CGPoint]())
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else {
return
}
guard var lastLine = lines.popLast() else {return }
lastLine.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
}
Canvas.swift文件
import Foundation
@objc(Canvas)
class Canvas: RCTViewManager {
override func view() -> UIView! {
return CanvasView()
}
override class func requiresMainQueueSetup() -> Bool {
return true
}
}
Canvas.m文件
#import <Foundation/Foundation.h>
#import "React/RCTViewManager.h"
@interface RCT_EXTERN_MODULE(Canvas, RCTViewManager)
@end
这是我的JS代码
const Canvas = requireNativeComponent('Canvas');
const App = () => {
return (
<View style={styles.container}>
<Canvas style={styles.bottom} />
</View>
);}
代码工作正常,但即使我对我的反应本机代码进行了一些更改并执行命令+s,fast refresh我仍然会收到错误消息Tried to register two views with the same name Canvas,但是当我运行时npx react-native run-ios,代码工作正常。hot reloading or fast refresh因此,即使我没有更改本机组件,当我包含我创建的本机组件时,总而言之不受支持。我可以理解如果我在 iOS swift 或客观 c 代码中更改某些内容,那么我需要重新运行 npx react-native run-ios,但即使在侧面没有更改iOS并且只在JS侧面进行更改后,我仍然会收到此错误。我的项目中没有两个同名的视图Canvas。我还尝试清除watchman、删除package.json和重新安装npm模块和pod
同样的问题也发生在android一边
