2016-03-15 62 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