Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从渲染常量设置状态_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 从渲染常量设置状态

Javascript 从渲染常量设置状态,javascript,reactjs,react-native,Javascript,Reactjs,React Native,在我的例子中,我想设置状态,feedbackLogId,这是调用函数所必需的,该函数将根据feedbackLogId获取反馈的详细信息。然而,常量是从上一个屏幕获得的,我只能在render()方法中设置常量 第一屏 在这里,我从WebAPI获取数据,并将其呈现在一个平面列表中,其中将有多个反馈 dataSource = [{ "feedbackLogId": 1, "name": "Test1", "comment

在我的例子中,我想设置状态,
feedbackLogId
,这是调用函数所必需的,该函数将根据
feedbackLogId
获取反馈的详细信息。然而,常量是从上一个屏幕获得的,我只能在render()方法中设置常量

第一屏 在这里,我从WebAPI获取数据,并将其呈现在一个平面列表中,其中将有多个反馈

dataSource = [{
  "feedbackLogId": 1,
  "name": "Test1",
  "comment": "Hello",
}, {
  "feedbackLogId": 2,
  "name": "Test2",
  "type": "Hi",
}, {
  "feedbackLogId": 3,
  "name": "Test3",
  "type": "Morning",
}]
当我按下其中一个反馈时,我希望导航到第二个屏幕,但保存feedackLogId的值,以便获得有关反馈的更多详细信息

<TouchableOpacity style={styles.listItem}
      onPress={() => this.props.navigation.navigate('FeedbackDetails', {value: item.feedbackLogId})}
      >
</TouchableOpacity>
我想在componentDidMount()中调用函数getPendingDetails,这就是我需要feedbackLogId的地方

componentDidMount() {
        //this is where I would need the state of feedbackLogId
        this.getPendingDetails(this.state.feedbackLogId);
    }
只有在render内部,我才能获得从另一个屏幕传递的数据的值

    const feedbackLogId = this.props.navigation.getParam('value', 'nothing')

我曾尝试在屏幕的其他部分调用
const feedbackLogId=this.props.navigation.getParam('value','nothing')
,但遇到了一些错误,例如
this.props
未定义。有没有办法解决这个问题?谢谢你的帮助

尝试使用getDerivedStateFromProps

static getDerivedStateFromProps = (props, state) => { 
    console.log('props',props.navigation)
    const feedbackLogId = props.navigation.getParam('value', 'nothing');
    if(feedbackLogId){
    this.getPendingDetails(feedbackLogId);
    }
  }  
在构造函数中

 this.getPendingDetails = getPendingDetails.bind(this) 


如果您遇到任何问题,请告诉我。它说this.getPendingDetails不是函数,this.getPendingDetails是未定义的
TypeError:TypeError:this.getPendingDetails不是函数。(在“this.getPendingDetails(feedbackLogId)”中,“this.getPendingDetails”未定义)
使用箭头函数我使用箭头函数的目的是什么?
 this.getPendingDetails = getPendingDetails.bind(this) 
 this.getPendingDetails = this.getPendingDetails.bind(this)