Skip to content
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

enable react-native >=0.60 #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## install

$ npm install native-echarts --save
$ npm install native-echarts react-native-webview --save

## Usage

Expand Down Expand Up @@ -80,6 +80,7 @@ screenshots:
![image](https://github.com/somonus/react-native-echarts/blob/master/example/demoIOS.png)

### Android
copy tpl.html to android\app\src\main\assets

Open the Android project in the android directory with Android Studio and click run.

Expand Down
28 changes: 19 additions & 9 deletions src/components/Echarts/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import React, { Component } from 'react';
import { WebView, View, StyleSheet, Platform } from 'react-native';
import { View, StyleSheet, Platform } from 'react-native';
import renderChart from './renderChart';
import WebView from 'react-native-webview';
import echarts from './echarts.min';

// 版本变更https://zhuanlan.zhihu.com/p/89494013
export default class App extends Component {

constructor(props) {
super(props);
this.state = {chart:null};
// refs 弃用了 https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs
this.chart = null;
this.setChart = element => {
this.setState({chart:element})
};
this.setNewOption = this.setNewOption.bind(this);
}


componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) {
this.refs.chart.reload();
static getDerivedStateFromProps(nextProps, prevState) {
if(nextProps.option !== prevState.option) {
prevState.chart && prevState.chart.reload();
}

return null;
}


setNewOption(option) {
this.refs.chart.postMessage(JSON.stringify(option));
this.state.chart && this.state.chart.postMessage(JSON.stringify(option));
}

render() {
return (
<View style={{flex: 1, height: this.props.height || 400,}}>
<WebView
ref="chart"
ref = {this.setChart}
scrollEnabled = {false}
injectedJavaScript = {renderChart(this.props)}
style={{
Expand All @@ -34,7 +43,8 @@ export default class App extends Component {
}}
scalesPageToFit={Platform.OS !== 'ios'}
originWhitelist={['*']}
source={require('./tpl.html')}
// source={require('./tpl.html')}
source= {Platform.OS === 'ios' ? require('./tpl.html') : { uri: 'file:///android_asset/tpl.html' }}
onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
/>
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Echarts/tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
width: 100%;
margin: 0;
padding: 0;
// overflow: hidden;
/* overflow: hidden; */

}
#main {
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component } from 'react';
import { WebView, View } from 'react-native';
import { Container, Echarts } from './components'

export default class App extends Component {
Expand Down