-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
组件封装思路 #40
Comments
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hooks中传入传出api的设计
比如我们rn app需要引入录音功能,确认需要引入一个react-native-sound库,
根据这个库的pai 我们自己设计一个useSound的hook,因为他本身没有提供对应的hooks
需要有什么功能,点击play方法,pause方法,录音的duration属性,播放时长做一个进度条,
我们需要考虑传入传出,传入先传一个考虑录音的url,
尽量少的回调函数的传入,比如我想在报错的时候进行提示,这种我们是不会在hooks里面直接提示的,但按以前我开发的思路,可能会传一个处理error的回调函数进去,会想到再useCallback一下,嗯,觉得很完美,但是其实hooks只是来帮你管理状态的,越多的回调的传入反而让hook显示臃肿,更优化的方法是先用一个useState新增一个error,来捕获存储代码过程中的报错,最后将之抛出。
这里获取error的设计,我是在自定义的play方法中接受第二个参数,进行return err,这样子
The text was updated successfully, but these errors were encountered: