如何在react-native中的导航选项中使用redux状态值?

时间:2018-05-15 10:51:05

标签: react-native

我想从存储在redux状态的对象设置navgationOptions标头。我尝试了以下方法,它说currentWorkout是未定义的。我该怎么做才能克服这个错误?使用来自redux?

的值设置navigationOption属性的正确方法是什么?
"\n54766392632990,178.32.243.13,wfsdsfsdfs23432,\n54766393632990,178.32.243.13,"
.split("\n")
.filter((n)=> n!== "")
 .map(n=> parseInt(n.split(",")[0]))

1 个答案:

答案 0 :(得分:1)

使用react-navigation 2.0, 我的猜测是你可能想做类似的事情:

class WorkoutDetailScreen extends Component {
  static navigationOptions = ({ navigation }) => {
    let workoutName = 'DEFAULT_VALUE'
    if (navigation.state.params) workoutName = navigation.state.params.workoutName
    return {    
      headerTitle: workoutName, 
      headerTitleStyle: {
        width: "100%",
        alignItems: "center"
      },
      headerStyle: {
        paddingRight: 10,
        paddingLeft: 10
      }
    };
  };

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { workoutName } = this.props.currentWorkout;
    this.props.navigation.setParams({
      params: { workoutName },
      key: this.props.navigation.state.key
    })
  }

  render() {
    return (
      <View>
        <Text>{this.props.currentWorkout.workoutName}</Text>
      </View>
    );
  }
}

const mapStateToProps = state => {
  return {
    currentWorkout: state.workouts.currentWorkout
  };
};

const mapDispatchToProps = dispatch => {
  return {
    getWorkout: bindActionCreators(getWorkout, dispatch)
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(
  WorkoutDetailScreen
)