You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
error Failed to build iOS project. We ran "xcodebuild"command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by opening demo_native.xcodeproj
// 父组件importChildfrom'path/to/Child'<View><Childname='jiaming'/></View>// 子组件render(){const{ name }=this.props;return(<View><Text>{name}</text></View>)}
2. 子组件传值给父组件
通过props的方法进行传值
// 父组件importChildfrom'path/to/Child'getValue(data){console.log(data);}render(){return(<View><ChildgetValue={this.getValue.bind()}></View>
)
}// 子组件trigger(){this.props.getValue({name: 'name from child component'});}render(){return(<View><TextonPress={this.trigger.bind(this)}> some awesome text </Text></View>)}
constructor(props){super(props);this.state={typeFlag: 1}}render(){return(<View>{typeFlag==1 ?
<Text>type flag is one</Text>
:
typeFlag==2 ?
<Text>type flag is two</Text>
:
null}</View>)}
staticnavigationOptions=({navigation})=>({headerLeft: (<TouchableOpacityonPress={this.handleMothed.bind(this)}><Textstyle={{color: '#999',paddingLeft: 10}}>返回</Text></TouchableOpacity>)});handleMothed(){console.log('this is a demo');}
你应该这样写(使用this.props.navigation.setParams):
staticnavigationOptions=({navigation})=>({headerRight: (<TouchableOpacityonPress={()=>navigation.state.params.handleComfirm()}><Textstyle={{color: '#999',paddingLeft: 10}}>确认</Text></TouchableOpacity>))});componentDidMount(){// 组件加载完之后操作this.props.navigation.setParams({handleComfirm:this.handleComfirm})}handleComfirm(){console.log('this is a demo');}
最近使用
react-native
参与开发了个应用,记录下其中踩的一些坑。本人使用的是mac电脑进行开发,本文仅对此平台进行记录📝下载不了xcode
升级自己的mac的系统到最新版本,之后在mac的应用市场中下载xcode。
请保持网络的顺畅,升级系统和下载安装
xcode
比较耗时,需耐心等待啦~ran "xcodebuild" command, error code for 65
跑起来的时候可能会出现这样的错误:
可以参考下面的步骤解决:
File -> Open -> demo_native -> ios
File -> Project Setting -> Advanced -> Custom -> Relative to workspace
react-native run-ios
后没反应在完成了相关的下载之后,运行react-native项目之后过两三分钟还是没有反应,请确保你的网络良好并重新运行,等待上一段时间。
首次跑应用耗时比较长,第二次跑的话就很快了~
出现
Entry, ":CFBundleIdentifier", Does Not Exist
错误在执行
react-native run-ios
后出现Entry, ":CFBundleIdentifier", Does Not Exist
,可以通过更改文件的设置来解决,xcode打开文件目录后,File -> Project Setting -> Advanced -> Custom -> Relative to workspace
。如果还是不行,请使用好点的翻墙工具(比如使用Shadowsocks,而不是蓝灯)。实现页面的热加载
在项目启动之后,mac上的模拟器已经开启了,但是修改保存ide上的代码后,模拟器并不能够实现热加载。这个时候应该开启模拟器的项目热加载功能。做法如下:
模拟器中进入正在执行的项目,然后在mac上按住
command + d
就可以调出对话框,选择Enable Live Reload
就可以了。关联浏览器调试
运行项目之后,可以在浏览器内对应用的js进行调试。调试操作:
http://localhost:8081/debugger-ui/
command+d -> Debug JS Remotely
基础要点
props(属性)和state(状态)
props
是在父组件中指定,在被指定的组件生命周期中不做改变。如果需要改变的数据,则可以使用state
。this的绑定
ES6中自定义的函数里面使用
this
关键字,需要对其进行绑定操纵,否则this
的指向会指向空。操作示例:样式编写的几种方式
1. 使用类名
2. 使用行内样式
3. 类名和行内样式结合
父子组件的传值
这里没有引入状态管理工具,比如redux。
1. 父组件传值给子组件
2. 子组件传值给父组件
父组件的数据状态变动,子组件的数据没变动?
在父组件中设置了父组件的数据变动了,但是子组件的数据并没有变动。比如:
出现在子组件中数据没变动的话,应该在子组件中做下面的处理:
相关的案例如下:
setState中引入变量
在我们改变state值的时候,我们一般都会使用到
setState
,比如:上面的
setState
中的key值是name,那么,如果我使用一个变量代替name需要怎么写呢?答:使用中括号
[]
来包裹就行了。如下demorender return
内的条件判断写法在
View
页面内,很多时候是需要你根据条件判断进行,那么相关的写法你可以包裹在一个大括号{}
里面的。比如:使用Modal会遮住下层的内容
在开始时候,引入
Modal
之后是,执行了相关的代码,弹出了Modal
之后,是看不到下层的内容的,这很是不合理。解决方案:引入属性transparent
。如下:
javascript的引入方式
1. 行内编写
2. 行内引入指针
navigation内的事件编写
在
react native navigation
中直接使用类似this.handleMethod
这种方法是不生效的,比如:你应该这样写(使用
this.props.navigation.setParams
):mac上安装的安卓adb
使用brew安装。
参考链接:https://www.jianshu.com/p/bb74ae9d26f6
后话
文章部分内容的引用已丢失,若有雷同,不胜荣幸。如有错误,还望看官纠正。
更多的内容请前往我的博客
The text was updated successfully, but these errors were encountered: