简体   繁体   English

Redux状态更新不会传递到组件的无状态函数中的方法

[英]Redux state updates don't pass through to a method within my component's stateless function

I have a stateless function that returns a component for my Redux/React Native application. 我有一个无状态函数,该函数为Redux / React Native应用程序返回一个组件。 In it are some defined helper functions: 其中包含一些已定义的辅助函数:

export default function TasksList (props) {
  let ds = new ListView.DataSource({
    rowHasChanged: (r1, r2) => r1 !== r2
  });

  const _onLongPress = (rowData) => {
    props.changeCurrentlyEditedTask(rowData.index, rowData)

    props.navigator.push({
      component: EditTask,
      title: props.selectedTaskText,
      leftButtonTitle: 'Cancel',
      rightButtonTitle: 'Save',
      onLeftButtonPress: () => _cancelEditingTask(),
      onRightButtonPress: () => _saveEditedTask()
    })
  }

  const _cancelEditingTask = () => {
    props.navigator.pop();
    props.resetSelectedTask();
  }

  const _saveEditedTask = () => {
    props.saveEditedTask()
    props.navigator.pop();
  }

  const dataSource = ds.cloneWithRows(props.listOfTasks);

  return (
    <View style={ styles.tasksListContainer }>
      <TextInput
        autoCorrect={ false }
        onChangeText={ (text) => props.onChangeText(text) }
        onSubmitEditing={ () => props.addTask(props.text) }
        returnKeyType={ 'done' }
        style={ styles.tasksListTextInput }
        value={ props.text } />
      <ListView
        automaticallyAdjustContentInsets={ false }
        dataSource={ dataSource }
        enableEmptySections={ true }
        renderRow={ (rowData, secctionID, rowID) => {
          return (
            <TasksListCell
              completed={ rowData.completed }
              id={ rowID }
              onLongPress={ (rowID) => _onLongPress(rowData) }
              onPress={ (rowID) => props.changeCompletionStatus(rowData.index) }
              text={ rowData.text }
              completed={ rowData.completed }
              formattedDate={ rowData.hasOwnProperty('formattedDate') ? rowData.formattedDate : undefined } />
          )
        }
      }
      style={ styles.tasksListView } />
    </View>
  );
};

Whenever my Redux state is changed, its changes proliferate through to TasksList as intended. 每当我的Redux状态更改时,其更改就会按预期扩散到TasksList。 However, _saveEditedTask fails to receive those updates. 但是, _saveEditedTask无法接收这些更新。

If I attempt to pass props as an argument to my saveEditedTask dispatch method in my container, it does not receive the updated state that the parent TasksList function does. 如果我试图通过props作为参数传递给我的saveEditedTask在我的容器调度方法,没有收到父更新的状态TasksList功能一样。

How can I fix this code so that _saveEditedTask receives the most up-to-date props from its parent TasksList function? 如何修复此代码,以使_saveEditedTask从其父TasksList函数接收最新的props

At the moment the _saveEditedTask function is defined and in fact does have access to the props of the TasksList component. 目前,已定义_saveEditedTask函数,并且实际上可以访问TasksList组件的道具。

The issue is that you are not passing this function as a prop to any children in the render method. 问题是您没有将此函数作为道具传递给render方法中的所有子项。

You should give the below prop to one of the children in the render method of your component. 您应该在组件的render方法中将以下属性提供给子级之一。 If you don't do this then the function will never be invoked. 如果您不这样做,则该函数将永远不会被调用。

onSave={ () => _saveEditedTask() }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM