React-native/react-navigation:如何从`static navigationOptions`访问组件的状态?

stk*_*flw 15 reactjs react-native react-navigation

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

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>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*ter 15

简单的设计模式

正如@ val的优秀答案的后续跟进一样,这就是我如何构建我的Component以便所有参数都设置在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...
}
Run Code Online (Sandbox Code Playgroud)

  • 这正是我所需要的 - 谢谢! (2认同)

Val*_*Val 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);
}
Run Code Online (Sandbox Code Playgroud)

现在你可以拥有一个与组件相关的按钮处理程序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} />
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这次真是万分感谢.这确实应该包含在反应导航的文档中.我个人认为React Native通过弃用导航来支持这个库而造成了损害.它仍然很早,在大面积感觉破碎. (4认同)

Cod*_*ngh -5

您收到此错误是因为您在声明之前使用了 props 和 state 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',
  }
  }
Run Code Online (Sandbox Code Playgroud)

干杯:)