<bdo id='696UH'></bdo><ul id='696UH'></ul>
  1. <i id='696UH'><tr id='696UH'><dt id='696UH'><q id='696UH'><span id='696UH'><b id='696UH'><form id='696UH'><ins id='696UH'></ins><ul id='696UH'></ul><sub id='696UH'></sub></form><legend id='696UH'></legend><bdo id='696UH'><pre id='696UH'><center id='696UH'></center></pre></bdo></b><th id='696UH'></th></span></q></dt></tr></i><div id='696UH'><tfoot id='696UH'></tfoot><dl id='696UH'><fieldset id='696UH'></fieldset></dl></div>
    <legend id='696UH'><style id='696UH'><dir id='696UH'><q id='696UH'></q></dir></style></legend>

    <small id='696UH'></small><noframes id='696UH'>

    <tfoot id='696UH'></tfoot>

    1. 来自自定义组件的 React Native Change State

      React Native Change State from Custom Component(来自自定义组件的 React Native Change State)

        1. <i id='vFvPZ'><tr id='vFvPZ'><dt id='vFvPZ'><q id='vFvPZ'><span id='vFvPZ'><b id='vFvPZ'><form id='vFvPZ'><ins id='vFvPZ'></ins><ul id='vFvPZ'></ul><sub id='vFvPZ'></sub></form><legend id='vFvPZ'></legend><bdo id='vFvPZ'><pre id='vFvPZ'><center id='vFvPZ'></center></pre></bdo></b><th id='vFvPZ'></th></span></q></dt></tr></i><div id='vFvPZ'><tfoot id='vFvPZ'></tfoot><dl id='vFvPZ'><fieldset id='vFvPZ'></fieldset></dl></div>
          <legend id='vFvPZ'><style id='vFvPZ'><dir id='vFvPZ'><q id='vFvPZ'></q></dir></style></legend>

          <small id='vFvPZ'></small><noframes id='vFvPZ'>

            <tbody id='vFvPZ'></tbody>
          • <bdo id='vFvPZ'></bdo><ul id='vFvPZ'></ul>
              • <tfoot id='vFvPZ'></tfoot>

                本文介绍了来自自定义组件的 React Native Change State的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有自定义选择器组件,我想更新 selectedValue 和 onValueChange 事件的状态.如果我直接在渲染部分添加选择器,它工作正常,但由于代码重复,我决定将我的选择器转换为自定义组件.所以我这样做了:

                I have custom picker component and i want to update states on selectedValue and onValueChange event. It works fine if i add picker directly in render section but due to duplication of code i decided to convert my pickers into custom component. So i did it like this:

                    import React, { Component } from 'react';
                import {
                    StyleSheet,
                    View,
                    Modal,
                    ActivityIndicator
                } from 'react-native';
                import { Item, Picker } from 'native-base'
                
                const CustomPicker = props => {
                    const {
                        pickerLabel,
                        selectedVal,
                        onChangeVal,
                        pickerStyle,
                        pickerData,
                        ...attributes
                    } = props;
                
                    return (
                        <Picker
                            selectedValue={selectedVal}
                            style={[pickerStyle]}
                            onValueChange={(itemValue, itemIndex) => this.setState({onChangeVal: itemValue})}>
                            <Picker.Item label={pickerLabel} value={''}/>
                            {pickerData.map((item, key) => (
                                <Picker.Item label={item} value={item} key={key}/>)
                            )}
                        </Picker>
                    )
                }
                
                const styles = StyleSheet.create({
                
                });
                
                export default CustomPicker;
                

                我像这样调用不同的组件:

                I call on different component like this:

                <CustomPicker pickerLabel={"Select User"} selectedVal={this.state.selectedUser} pickerStyle={styles.picker} pickerData={this.state.userList} onChangeVal={this.state.selectedUser} />
                

                标签 + 数据很好地填充到选择器中,但事件出现了问题.它没有更新拾取数据的状态,我错过了什么?

                Label + data is populating in picker nicely but issue is coming on events. Its not updating state on picking data, What am i missing?

                推荐答案

                您需要在父组件中创建一个函数来更改值,因为您正在尝试更改 state无状态组件

                You need to make a function in the parent component to change the values, since you're trying to change the state in a stateless component

                相反,你需要做的是

                父组件

                onChange = (itemValue, itemIndex) => {
                  // Set the state here and update as required
                }
                
                <CustomPicker pickerLabel={"Select User"} selectedVal={this.state.selectedUser} pickerStyle={styles.picker} pickerData={this.state.userList} onChangeVal={this.onChange} />
                

                子组件

                onValueChange={onChangeVal}>
                

                对于多个选择器:

                父组件

                state = {
                  user: [
                         {value: 'Something', key: 0, label: 'Select User', userList: [//...Some Array here]},
                         {value: 'Something Else', key: 1, label: 'Dont Select User', userList: [//...Some Array here]}
                    ]
                }
                
                onChange = (itemValue, itemIndex, pickerIndex) => {
                      // Update the states based on the pickerIndex
                    }
                
                const {user} = this.state
                
                // Use a map here
                
                user.map((data, index) => (
                      <CustomPicker key={index} index={index} pickerLabel={data.label} selectedVal={data.value} pickerStyle={styles.picker} pickerData={data.userList} onChangeVal={this.onChange} />
                ))
                

                子组件

                const {
                        index, <== Picker Index
                        pickerLabel,
                        selectedVal,
                        onChangeVal,
                        pickerStyle,
                        pickerData,
                        ...attributes
                    } = props;
                
                <Picker
                            selectedValue={selectedVal}
                            style={[pickerStyle]}
                            onValueChange={(itemValue, itemIndex) => this.props.onChangeVal(itemValue, itemIndex, index)}> <== Pass picker value to the parent 
                

                这篇关于来自自定义组件的 React Native Change State的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                    <bdo id='YYfz9'></bdo><ul id='YYfz9'></ul>

                    • <tfoot id='YYfz9'></tfoot>

                        <small id='YYfz9'></small><noframes id='YYfz9'>

                        <i id='YYfz9'><tr id='YYfz9'><dt id='YYfz9'><q id='YYfz9'><span id='YYfz9'><b id='YYfz9'><form id='YYfz9'><ins id='YYfz9'></ins><ul id='YYfz9'></ul><sub id='YYfz9'></sub></form><legend id='YYfz9'></legend><bdo id='YYfz9'><pre id='YYfz9'><center id='YYfz9'></center></pre></bdo></b><th id='YYfz9'></th></span></q></dt></tr></i><div id='YYfz9'><tfoot id='YYfz9'></tfoot><dl id='YYfz9'><fieldset id='YYfz9'></fieldset></dl></div>

                          <tbody id='YYfz9'></tbody>
                        <legend id='YYfz9'><style id='YYfz9'><dir id='YYfz9'><q id='YYfz9'></q></dir></style></legend>