尝试构建一个React Native应用程序,该应用程序在"共享"菜单(Android的"共享操作","iOS的共享扩展")中注入一个菜单项,并在应用程序中接收共享项.是否有一个组件,如果没有,最好的方法是什么?
我为此实现了一个模块:https://www.npmjs.com/package/react-native-share-menu(目前仅适用于Android).
这是如何使用它:
安装模块:
npm i --save react-native-share-menu
在android/settings.gradle中:
...
include ':react-native-share-menu', ':app'
project(':react-native-share-menu').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-share-menu/android')
Run Code Online (Sandbox Code Playgroud)
在android/app/build.gradle中:
...
dependencies {
...
compile project(':react-native-share-menu')
}
Run Code Online (Sandbox Code Playgroud)
注册模块(在MainActivity.java中):
import com.meedan.ShareMenuPackage; // <--- import
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ShareMenuPackage() // <------ add here
);
}
......
}
Run Code Online (Sandbox Code Playgroud)
例:
import React, {
AppRegistry,
Component,
Text,
View
} from 'react-native';
import ShareMenu from 'react-native-share-menu';
class Test extends Component {
constructor(props) {
super(props);
this.state = {
sharedText: null
};
}
componentWillMount() {
var that = this;
ShareMenu.getSharedText((text :string) => {
if (text && text.length) {
that.setState({ sharedText: text });
}
})
}
render() {
var text = this.state.sharedText;
return (
<View>
<Text>Shared text: {text}</Text>
</View>
);
}
}
AppRegistry.registerComponent('Test', () => Test);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5178 次 |
最近记录: |