React-Native TextInput,用于编辑动态对象键:值

时间:2018-02-14 18:13:55

标签: react-native redux

我正在使用Redux处理React-Native。其中一个组件根据在其中传递给它的对象中的元素呈现一系列TextInput,这些元素是从应用程序中较早的API读入的。

我希望能够编辑该值并在状态中更新它。对象的键,值和长度都是动态的。我的所有redux动作和减速器都能正常工作,我的组件正在接收它的道具,所以我试着将它归结为一个非常简单的例子。为了说明我的问题,让我们说组件在其道具中接收到这个对象:

 item:{
   type: monitor,
   make: HP,
   tag_number: 005
 }

我的组件的render方法创建并显示textinputs:

render(){
   var item_views = []
   for(index in this.props.item) {
     item_views.push(
          <View key={index} style={styles.rowContainer}>
          <Text style={styles.label}>{index} : </Text>
          <TextInput 
             style={styles.input_box}
             onSubmitEditing={(text) => this._update(index, text)}
             defaultValue={this.props.item[index].toString()}
             placeholder={index}>
          </TextInput>
          </View>
     )
   }
   return (
     <View>
     {item_views}
     </View>
   )
 }

 _update(index, text){
    //This is where I dispatch the action to change the value of "item[index]" to "text"
 }

这个几乎有效,除了&#34; index&#34;,它始终在渲染方法中的各个TextInputs中正确显示在&#34; _update&#34;函数总是解析为对象中的顶部键,即&#34; type&#34;在这个例子中,无论我编辑哪个文本输入。如何通过textinput的onSubmitEditing函数将正确的索引传递给我的更新函数?

2 个答案:

答案 0 :(得分:1)

尝试使用&#34;让&#34;在你的for循环中

ALTER PROCEDURE [dbo].[myproc]
@aCode VARCHAR(50),
@bCode VARCHAR(50),
@ClosingPrice DECIMAL(28,4),
@specialDate DATETIME,
@person VARCHAR(50),
@priced INT = NULL
AS
    SET @specialDate = LEFT(CONVERT(VARCHAR, @specialDate, 112), 8)
    SET @priced = (SELECT CASE WHEN @ClosingPrice > 0 THEN 1 ELSE 0 END)
BEGIN 
    DECLARE @ID INT
   IF NOT EXISTS(SELECT sec.ID
        FROM dbo.Securities sec
        WHERE sec.aCode = @aCode) SET @aCode = @bCode;

    DECLARE db_cursor CURSOR FOR
        SELECT sec.ID
        FROM dbo.Securities sec
        WHERE sec.aCode = @aCode

    OPEN db_cursor

    FETCH NEXT FROM db_cursor INTO @ID

    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF EXISTS(SELECT 1 FROM Prices WITH (nolock)
              WHERE DATEDIFF(YEAR, @specialDate, specialDate) = 0
                AND DATEDIFF(MONTH, @specialDate, specialDate) = 0
                AND DATEDIFF(DAY, @specialDate, specialDate) = 0
                AND ID = @ID)
        BEGIN
        -- Update
            UPDATE dbo.Prices
            SET ClosingPrice = @ClosingPrice,
                UpdatedDate = GETDATE(),
                DownloadFileID = null,
                UpdatedByUser = @person,
                Priced = @priced
            WHERE
                DATEDIFF(YEAR, @specialDate, specialDate) = 0
                AND DATEDIFF(MONTH, @specialDate, specialDate) = 0
                AND DATEDIFF(DAY, @specialDate, specialDate) = 0
                AND ID = @ID
        END
        ELSE
        BEGIN
        -- Insert
            INSERT INTO dbo.Prices (ID, ClosingPrice, specialDate, UpdatedDate, UpdatedByUser, Priced)
            VALUES (@ID, @ClosingPrice, @specialDate, GETDATE(), @person, @priced)
        END

        FETCH NEXT FROM db_cursor INTO @ID;
    END

    CLOSE db_cursor
    DEALLOCATE db_cursor
END

在您的案例中索引和item_views都是全局对象,因此索引将始终具有item_views中的最终值(无论它是什么)

答案 1 :(得分:1)

您可以使用函数式编程:

render(){
   const { items } = this.props;
   return (
     <View>
     {items.map((el,index) => (
        <View key={some_id} style={styles.rowContainer}>
          <Text style={styles.label}>{index} : </Text>
          <TextInput 
             style={styles.input_box}
             onSubmitEditing={(text) => this._update(index, text)}
             defaultValue={el.toString()}
             placeholder={index}>
          </TextInput>
        </View>
     ))}
     </View>
   )
 }

 _update(index, text){
    //This is where I dispatch the action to change the value of "item[index]" to "text"
 }

请勿在组件密钥上使用索引:https://reactjs.org/docs/lists-and-keys.html#keys