17

例如,当您有一个表单组件并且需要使用导航栏中的按钮提交组件的一部分状态时,您如何处理这种情况?

const navBtn = (iconName, onPress) => (
  <TouchableOpacity
    onPress={onPress}
    style={styles.iconWrapper}
  >
    <Icon name={iconName} size={cs.iconSize} style={styles.icon} />
  </TouchableOpacity>
)

class ComponentName extends Component {

  static navigationOptions = {
    header: (props) => ({
      tintColor: 'white',
      style: {
        backgroundColor: cs.primaryColor
      },
      left: navBtn('clear', () => props.goBack()),
      right: navBtn('done', () => this.submitForm()), // error: this.submitForm is not a function
    }),
    title: 'Form',
  }

  constructor(props) {
    super(props);
    this.state = {
      formText: ''
    };
  }

  submitForm() {
    this.props.submitFormAction(this.state.formText)
  }

  render() {
    return (
      <View>
        ...form goes here
      </View>
    );
  }
}
4

4 回答 4

15

简单的设计模式

作为@val 出色答案的后续行动,这就是我构建组件的方式,以便所有参数都设置在componentWillMount. 我发现这使它更简单,并且是所有其他屏幕的简单模式。

static navigationOptions = ({navigation, screenProps}) => {
  const params = navigation.state.params || {};

  return {
    title:       params.title,
    headerLeft:  params.headerLeft,
    headerRight: params.headerRight,
  }
}

_setNavigationParams() {
  let title       = 'Form';
  let headerLeft  = <Button onPress={this._clearForm.bind(this)} />;
  let headerRight = <Button onPress={this._submitForm.bind(this)} />;

  this.props.navigation.setParams({ 
    title,
    headerLeft,
    headerRight, 
  });
}

componentWillMount() {
  this._setNavigationParams();
}

_clearForm() {
  // Clear form code...
}

_submitForm() {
  // Submit form code...
}
于 2017-07-22T18:16:24.580 回答
9

使用 发送绑定函数setParams,然后您将可以访问state该函数中的组件。

例子:

constructor(props) {
    super(props);
    this._handleButtonNext = this._handleButtonNext.bind(this);
    this.state = { selectedIndex: 0 }
}

componentDidMount() {
    this.props.navigation.setParams({
        handleButtonNext: this._handleButtonNext,
    });
}

_handleButtonNext() {
    let action = NavigationActions.setParams({
        params: { selectedImage: images[this.state.selectedIndex] }
    });
    this.props.navigation.dispatch(action);
}

现在您可以拥有一个与组件相关的按钮处理程序state

static navigationOptions = ({ navigation }) => {
    const { state, setParams, navigate } = navigation;
    const params = state.params || {};

    return {
        headerTitleStyle: { alignSelf: 'center' },
        title: 'Select An Icon',
        headerRight: <Button title='Next' onPress={params.handleButtonNext} />
    }
}
于 2017-07-14T08:22:04.763 回答
1

在您的 componentDidMount 上,您可以使用

this.navigation.setParams({
 myTitle: this.props.myTitle
})

然后,在静态道具上将一个函数传递给您的标题。这个函数可以访问你之前设置的参数

感谢rafaelcorreiapoli

于 2017-04-14T05:07:04.840 回答
-5

您收到此错误是因为您在声明constructor(). 与构造函数一样,我们首先调用 super(props) 以便我们可以在组件中使用 props。请执行以下操作以获得所需的结果。

constructor(props) {
    super(props);
    this.state = {
      formText: ''
    };

  static navigationOptions = {
    header: (props) => ({
      tintColor: 'white',
      style: {
        backgroundColor: cs.primaryColor
      },
      left: navBtn('clear', () => props.goBack()),
      right: navBtn('done', () => this.submitForm()), // error: this.submitForm is not a function
    }),
    title: 'Form',
  }
  }

干杯:)

于 2017-04-13T19:32:32.433 回答