【问题标题】:Passing values after clicking on the Button - React Native单击按钮后传递值 - React Native
【发布时间】:2019-08-05 11:52:49
【问题描述】:

点击发送按钮后,如何在 Render() 方法中传递所有值?在这个 Render() 方法中,我返回了一些平面列表、视图、签名等。那么,是否可以通过单击按钮将所有这些值传递到另一个页面。

如果您的问题不清楚,请告诉我,以便我添加更多解释。

代码见下文(已编辑)。

感谢任何建议或帮助!

编辑:

    renderTextandInputs = (obje) => {
    var keyvalue_to_json = JSON.parse(obje.keyValues);
    var foundTextFields = [];
    for (let i = 0; i < keyvalue_to_json.length; i++) {
      if (keyvalue_to_json[i].type === 'textfield') {
        foundTextFields.push(<TextInput style={{ borderWidth: 1, flex: 1, alignItems: 'flex-start' }}>{keyvalue_to_json[i].placeholderText}</TextInput>)
      }
    }
    return (
      <View>
        <ScrollView>
          <ListItem
            title={obje.name}
            subtitle={obje.description}
          />

        </ScrollView>
        <View >
          {foundTextFields}
        </View>
      </View>

    )
  }


  render() {

    const obj = this.props.navigation.state.params.item;
    var propsArray = [];
    const itemArray = Object.assign(obj)
    propsArray.push(itemArray)

    keyExtractor = (item, index) => {
      return index.toString();
    }

    return (
      <View>
        <View>
          <FlatList
            key={this.keyExtractor}
            data={propsArray}
            renderItem={({ item }) => this.renderTextandInputs(item)}
          />
        </View>    
        <View >
          {this.state.signature ? (
            <Image
              resizeMode={"contain"}
              source={{ uri: this.state.signature }}
            />
          ) : null}
        </View>

        <Modal isVisible={this.state.isModalVisible}
          onBackdropPress={() => this.setState({ isModalVisible: false })}
        >       
          <Signature
            width="100"
            onOK={this.handleSignature}
            descriptionText="Sign"
            clearText="Clear"
            confirmText="Save"
            webStyle={style}
          />
        </Modal>

        <View>
          <Button title="SIGN" onPress={this._toggleModal} />
        </View>

        <View>
          <Button title="Send" onPress={this._onSendDoc} />
        </View>

      </View>
    );
  }

_onSendDoc = (item) => {
    this.props.navigation.navigate('Detail', { item: item })
  }
}

【问题讨论】:

  • 您提到的“发送”按钮在哪里?到目前为止,您尝试了哪些允许单击按钮将数据发送到下一页的方法?这应该很简单,只需将数据作为道具传递给下一个组件/页面
  • @TyroHunter 我还没试过。我知道我不能在 Render() 中有参数,所以我需要在按钮中使用哪些参数:onPress={() => this.onPressItem()}

标签: react-native button navigation render


【解决方案1】:

如果您在此处查看:https://facebook.github.io/react-native/docs/flatlist,您可以像这样为每个平面列表项呈现一个按钮:

编辑

_onSendAll = () => {
  const obj = this.props.navigation.state.params.item;
  var propsArray = [];
  const itemArray = Object.assign(obj)
  propsArray.push(itemArray)

  this.props.navigation.navigate("Detail", { allData: propsArray });
};


_onSendDoc = item => {
  this.props.navigation.navigate("Detail", { item: item });
};

render() {
  return (
    <FlatList
      data={[{title: 'Title Text', key: 'item1'}]}
      renderItem={({item}) => (
        <TouchableHighlight
          onPress={() => this._onSendDoc(item)}
          <View style={{backgroundColor: 'white'}}>
            <Text>{item.title}</Text>
          </View>
        </TouchableHighlight>
      )}
    />
  )

每点击一个按钮,传递的项目数据都会被记录。

【讨论】:

  • 我已经编辑了我的代码,你能检查一下并告诉我你将如何传递所有这些数据吗?我知道如何通过前任。 ListItem 的数据,但这里我有 ListItem,它呈现一些文本、输入字段,并且我有一个签名作为图像。
  • 似乎发生了很多解析,您能否发送obje.keyValues 的示例值?
  • 那是因为 keyValues 是一个字符串,所以我不得不解析它们,这里是解析前的日志输出示例:'[ { "type": "textfield", "placeholderText" : "Enter your full name ", "title" : "名称" }]'
  • 问题是我不想将这些数据传递到另一个屏幕,而是使用 fetch() 将它们发布到服务器。
  • 阅读本文,您会明白我的意思。 facebook.github.io/react-native/docs/textinput。我的意思是也试试这个,直到你能够存储用户在 this.state 中输入的内容
猜你喜欢
  • 1970-01-01
  • 2018-01-03
  • 2018-08-25
  • 1970-01-01
  • 2017-07-29
  • 2013-06-28
  • 1970-01-01
  • 2021-01-23
  • 2020-09-14
相关资源
最近更新 更多