开发者问题收集

如何在屏幕之间成功传递参数?

2021-01-03
58

我阅读了很多 SO 和 GitHub 问题上的答案,试图实施解决方案,但无法针对这种情况提出解决方案。

这是我的代码的表示:

class MeetingsScreen extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      upcomingMeeting: [],
      refreshing: false
    };
  }

  async handlePress() {
    const user = await AsyncStorage.getItem('User');
    this.props.navigation.navigate('WriteSummary', { id: user.id, type: 'submit' });
  }

  printUpcomingMeetings = () => {
    return (<View style={styles.meeting}>
            <Button
              containerStyle={styles.meetingButton}
              style={styles.meetingButtonText}
              onPress={() => this.handlePress(user.id)}
              Press me to write summary!
            </Button>
          </View>);
    }

  }

  render () {
    return (<View style={{flex:1}} key={this.state.refreshing}>
    { this.printUpcomingMeetings() }
    </View>);
  }
}

class WriteSummaryScreen extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      storageId: '',
      normalId: -1,
      type: '',
      curSummary: ''
    }
  }

  componentDidMount = () => {
    const { params } = this.props.navigation.state;
    const { id, type } = params ? params : null;
    const storageId = 'summary_' + id;
    this.setState({storageId:storageId});
    this.setState({normalId:id});
    this.setState({type:type});
    AsyncStorage.getItem(storageId).then((value) => this.setSkipValue(value));
  }

  async setSkipValue (value) {
    if (value !== null) {
      this.setState({ 'curSummary': value });
    } else {
      this.setState({ 'curSummary': '' });
    }
  }

  async saveSummary (text) {
    this.setState({'curSummary': text});
    await AsyncStorage.setItem(this.state.storageId, text);
  }

  async handleSubmit() {
    const user = await AsyncStorage.getItem('User');
    if (this.state.type === 'submit') {
      // post insert
      const postres = fetch (url + '/create-summary', {
        method: 'POST',
        body: JSON.stringify({
          AppointmentId: this.state.normalId,
          SummaryText: this.state.curSummary,
          UserId: user.Id
        }),
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        }
      })
      .catch((error) => {
        console.error(error);
      });
    } else {
      // post update
      const postres = fetch (url + '/update-summary', {
        method: 'POST',
        body: JSON.stringify({
          AppointmentId: this.state.normalId,
          SummaryText: this.state.curSummary,
          UserId: user.Id
        }),
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        }
      })
      .catch((error) => {
        console.error(error);
      });
    }
    this.props.navigation.navigate('Meetings');
  }

  render () {
    return <View style={{flex:1}}>
      <View>
        <View style={{height:25, backgroundColor: colors.vikingBlue}}></View>
        <View style={{height:30, backgroundColor: colors.white}}></View>
        <View style={{flexDirection:'row', backgroundColor: colors.white, alignItems:'center'}}>
          <View style={{width:5}}></View>
          <TouchableOpacity onPress={() => this.props.navigation.goBack()} activeOpacity={0.5}>
            <Image style={{width:30, height:30}} source={require('./assets/icons8-back-50.png')} />
          </TouchableOpacity>
          <View style={{width:10}}></View>
          <View style={{width:mainTitleWidth,textAlign:'center',alignItems:'center'}}>
            <Text style={{fontSize:22}}>Settings</Text>
          </View>
          <TouchableOpacity onPress={() => this.props.navigation.navigate('HelpModal')} activeOpacity={0.5}>
            <Image style={{width:30, height:30}} source={require('./assets/help.png')} />
          </TouchableOpacity>
        </View>
        <View style={{height:30, backgroundColor: colors.white}}></View>
      </View>
      <TextInput
      multiline
      numberOfLines={6}
      style={styles.summaryInput}
      onChangeText={text => saveSummary(text)}
      value={this.state.curSummary} />
      <Button
        containerStyle={styles.summaryButton}
        style={styles.summaryButtonText}
        onPress={this.handleSubmit()}>
        Submit
      </Button>
    </View>
  }
}

function HomeStack() {
  return (
    <Tab.Navigator
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Meetings" component={MeetingsScreen} />
        <Tab.Screen name="Topics" component={TopicsScreen} />
    </Tab.Navigator>
  );
}

export default class AppContainer extends React.Component {

  // Main rendering function. Always begins on the SplashScreen, which checks user login status and directs to Meetings. I left it out and the other Tab navigator screens for less clutter.

  render() {
    return (
        <NavigationContainer>
          <Stack.Navigator headerMode='none' initialRouteName='Splash'>
            <Stack.Screen name='Splash' component={SplashScreen} />
            <Stack.Screen name='Main' component={HomeStack} />
            <Stack.Screen name='WriteSummary' component={WriteSummaryScreen} />
          </Stack.Navigator>
        </NavigationContainer>
    );
  }
};

按下 MeetingsScreen 上的按钮并导航到 WriteSummaryScreen 后,我收到错误 TypeError:undefined 不是对象(评估'_this7.props.navigation.state.params)'

让我困惑的是,屏幕已导航到,因此应该传递数据。我遗漏了什么?

3个回答

如果是基于类的组件,则可以访问如下参数:

class WriteSummaryScreen extends React.Component {
     const {id, type} = this.props.route.params;
//........

对于功能组件:

const WriteSummaryScreen =({navigation, route})=>{
    const {id, type} = route.params;

//..........
}
Ketan Ramteke
2021-01-04

您可以使用 getParam 函数访问从屏幕传递的参数。

因此您可以使用此代码:

const id = this.props.navigation.getParam("id");
const type = this.props.navigation.getParam("type");
Mahdi
2021-01-04

最终为我工作的是:

848021452
cappycap
2021-01-04