今天去北京识物科技有限公司面试react native开发工程师,回来后有感而发,总结一下我和该公司技术交流得火花!
Q:TextInput中,如何动态获得TextInput得高度,以便适配整体页面布局。
A:这个问题,其实是在问你,React Native 之TextInput 高度自增长扩展实现,e.g.:
import React, {Component} from 'react';import {AppRegistry, TextInput, StyleSheet} from 'react-native';export default class AutoExpandingTextInput extends Component { // 构造 constructor(props) { super(props); // 初始状态 this.state = { text: '', height: 0 }; this.onChange = this.onChange.bind(this); } onChange(event) { console.log(event.nativeEvent); this.setState({ text: event.nativeEvent.text, height:event.nativeEvent.contentSize.height }); } onContentSizeChange(params){ console.log(params); } render() { return ( <TextInput {...this.props} //将组件定义的属性交给TextInput multiline={true} onChange={this.onChange} onContentSizeChange={this.onContentSizeChange} style={[styles.textInputStyle,{height:Math.max(35,this.state.height)}]} value={this.state.text} /> ); }}const styles = StyleSheet.create({ textInputStyle: { //文本输入组件样式 width: 300, height: 30, fontSize: 20, paddingTop: 0, paddingBottom: 0, backgroundColor: "grey" }});
在多行输入(multiline={true}
)的时候,可以通过onChange回调函数,获取内容的高度event.nativeEvent.contentSize.height
,然后修改内容的高度。
接下来修改index.iOS.js或者index.Android.js 如下:
import AutoExpandingTextInput from './AutoExpandingTextInput';class AwesomeProject extends Component { _onChangeText(newText) { console.log('inputed text:' + newText); } render() { return ( <View style={styles.container}> <AutoExpandingTextInput style={styles.textInputStyle} onChangeText={this._onChangeText} /> </View> ); }}const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5FCFF', borderWidth: 1, justifyContent: 'center', alignItems: 'center' }, textInputStyle: { //文本输入组件样式 width: 300, height: 50, fontSize: 20, paddingTop: 0, paddingBottom: 0, backgroundColor: "grey" }});
Q:网络请求来一个图片得大小是1400*1200,而在本地APP上我只想让其适配我得屏幕,这样该如何处理?
A:其实这个问题是在问,图片比例缩放以及bitMap转BitmapDrawable(其实也是一个坑)
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ImageView.ScaleType; /** * This example shows how to resize an image * @author FaYnaSoft Labs * */ public class Main extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout linearLayout = new LinearLayout(this); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = 640; int newHeight = 480; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // create the new Bitmap object Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); ImageView imageView = new ImageView(this); imageView.setImageDrawable(bmd); imageView.setScaleType(ScaleType.CENTER); linearLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); setContentView(linearLayout); } }
Q:当一个页面或者说组件中得一个属性发生变化,这是react native得机制是要重新渲染得,这个过程在生命周期中走哪几步?
A:说白了还是对生命周期得理解,我得博客以前曾写过,连接是:http://www.cnblogs.com/gdsblog/p/7348375.html
根据我趟过得坑,我回教你这样回答这个问题,当前页面(容器/组件)得状态发生变化,这个变化首先有两点,走得生命周期也不是完全相同:1.当state发生变化,这时,程序会走shouldComponentUpdate(这一步主要判断你的state是否发生变化)如果变化了,那么接着走下一个生命周期componentWillUpdate(这一个表示组件将要更新,你可以在更新前做一些操作) 之后就会走render(从新渲染组件)然后执行componentDidUpdate,完成。2.组件更新结束之后执行,在初始化render
时不执行,当props发生变化,那么程序走得流程,会和上面不一致,它会在该过程之前,首先执行一下componentWillRecivePros (这个生命周期函数,也许有人会陌生,
componentWillReceiveProps
void componentWillReceiveProps( object nextProps)
当props
发生变化时执行,初始化render
时不执行,在这个回调函数里面,你可以根据属性的变化,通过调用this.setState()
来更新你的组件状态,旧的属性还是可以通过this.props
来获取,这里调用更新状态是安全的,并不会触发额外的render
调用
componentWillReceiveProps: function(nextProps) { this.setState({ likesIncreasing: nextProps.likeCount > this.props.likeCount });}
)
然后会执行其它操作。
Q:react native如何和原生android通信(调用其SDK…参考他的写一下:http://m.blog.csdn.net/u013718120/article/details/55506238)
A:这个没想好如何回答
Q:你们用navigation做导航,那你说说tabnavigator的每一个tab如何添加事件,(完成该需求,假设该tab下得内容是登录后可见得,那么我们需要验证一下,当用户点击该tab时,它是否登录)
A:这个问题可以看我博客里的一篇文章,去修改navigation的源码,不过出现了bug,很苦恼,
最后补充一点,突然想起来,技术还问了一个问题,“在异步中(promise)如果发生异常,我们去try catch了这个异常”到最后是否可以那个什么什么什么….
今天突然看了javascript异步变成,里面有段话是这样描述的:
综上所述就是,异步用回掉中,try catch并不能捕获异步回掉中的异常!
,
不敢面对自己的不完美,总是担心自己的失败,