2016-03-15 63 views
0
_render() { 
    return (
     <Group> 
      <Shape d={"M160 160 A 45 45, 0, 0, 1, 115 205"} stroke="#000000" strokeWidth={3} /> 
      <Shape d={"M160 160 A 45 45, 0, 0, 1, 115 205"} stroke="#000000" strokeWidth={3} /> 
     </Group> 
    ); 
} 

在這裏,我想讓形狀標記是可變的。 這樣的如何react-native呈現可變視圖?

let shapes = [<Shape d={"M160 160 A 45 45, 0, 0, 1, 115 205"} stroke="#000000" strokeWidth={3} />, 
      <Shape d={"M160 160 A 45 45, 0, 0, 1, 115 205"} stroke="#000000" strokeWidth={3} />] 

function _render() { 
    return (
     <Group> 
      {shapes} 
     </Group> 
    ); 
} 

有時我想要改變形狀和再次呈現。 我能做什麼?

回答

0

觸發渲染的最簡單方法是setState。

'use strict'; 
import React, { Component, View, Text, StyleSheet, TouchableHighlight, ART, TouchableOpacity } from 'react-native'; 
let { Shape, Group, Surface} = ART; 
export default class Hello extends Component { 
    constructor() { 
    super(); 
    this.state = { 
     shapes: null 
    }; 
    this.onPress = this.onPress.bind(this); 
    } 
    onPress() { 
    this.setState({ 
     shapes: [<Shape d={"M160 160 A 45 45, 0, 0, 1, 115 205"} stroke="#000000" strokeWidth={3} />, 
     <Shape d={"M160 160 A 45 45, 0, 0, 1, 115 205"} stroke="#000000" strokeWidth={3} />] 
    }); 
    } 
    render() { 
    return (<View style={styles.container}> 
       <Surface width={200} height={200}> 
        <Group> 
        {this.state.shapes} 
        </Group> 
        </Surface> 
        <TouchableOpacity onPress={this.onPress}> 
         <Text> 
         Click Me 
         </Text> 
        </TouchableOpacity> 
       </View>); 
    } 
} 
const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    backgroundColor: 'white' 
    }, 
    content: { 
    margin: 10 
    } 
}); 
+0

我的回答,我想在我的question.I測試我的代碼的失敗第一,我的問題,然後我的代碼工作...... ,謝謝您! – DengFeng