响应本机组件中的类型错误未定义的名称属性

时间:2017-11-05 12:07:07

标签: react-native react-navigation react-native-flatlist

我收到未定义名称属性的类型错误我相信我的EmployeeEdit.js文件。当我触摸员工列表中的一个项目时,我收到此错误,而不是使用传递的参数转到员工编辑表单。我控制台登录并发现state.employeeForm没有从EmployeeFormReducer加载值{ name, phone, shift }。我不知道为什么。

这是错误

TypeError: Cannot read property 'name' of undefined

This error is located at:
    in Connect(EmployeeForm) (at EmployeeEdit.js:21)
    in RCTView (at View.js:113)
    in View (at Card.js:6)
    in Card (at EmployeeEdit.js:20)
    in EmployeeEdit (created by Connect(EmployeeEdit))
    in Connect(EmployeeEdit) (at SceneView.js:35)
    in SceneView (at CardStack.js:413)
    in RCTView (at View.js:113)
    in View (at createAnimatedComponent.js:134)
    in AnimatedComponent (at Card.js:28)
    in Card (at PointerEventsContainer.js:55)
    in Container (at CardStack.js:443)
    in RCTView (at View.js:113)
    in View (at CardStack.js:373)
    in RCTView (at View.js:113)
    in View (at CardStack.js:372)
    in CardStack (at CardStackTransitioner.js:110)
    in RCTView (at View.js:113)
    in View (at Transitioner.js:192)
    in Transitioner (at CardStackTransitioner.js:60)
    in CardStackTransitioner (at StackNavigator.js:48)
    in Unknown (at createNavigator.js:36)
    in Navigator (at createNavigationContainer.js:198)
    in NavigationContainer (at App.js:37)
    in Provider (at App.js:36)
    in App (at renderApplication.js:35)
    in RCTView (at View.js:113)
    in View (at AppContainer.js:102)
    in RCTView (at View.js:113)
    in View (at AppContainer.js:126)
    in AppContainer (at renderApplication.js:34) 

EmployeeEdit.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import _ from 'lodash';
import EmployeeForm from './EmployeeForm';
import { employeeUpdate } from '../actions';
import { Card, CardSection, Button } from './common';

class EmployeeEdit extends Component {
  componentWillMount() {
    _.each(this.props.navigation.state.params.employee, (value, prop) => {
      this.props.employeeUpdate({ prop, value });
    });
  }
  onButtonPress() {
    const { params } = this.props.navigation.state;
    const { name, phone, shift } = params.employee;
  }
  render() {
    return (
      <Card>
        <EmployeeForm {...this.props} />
        <CardSection>
          <Button onPress={() => this.onButtonPress.bind(this)}>
            Save Changes
          </Button>
        </CardSection>
      </Card>
    );
  }
}

const mapStateToProps = (state) => {
  const { name, phone, shift } = state.employeeForm;

  return { name, phone, shift };
};

export default connect(mapStateToProps, { employeeUpdate })(EmployeeEdit);

EmployeeList.js

import React, { Component } from 'react';
import { FlatList } from 'react-native';
import { connect } from 'react-redux';
//import { R } from 'ramda';
import _ from 'lodash';
import { employeesFetch } from '../actions';
import { HeaderButton } from './common';
import ListEmployee from './ListEmployee';


class EmployeeList extends Component {
  static navigationOptions = ({ navigation }) => ({
    headerRight: (
      <HeaderButton onPress={() => navigation.navigate('employeeCreate')}>
        Add
      </HeaderButton>
    )
  });

  componentWillMount() {
    this.props.employeesFetch();
  }

  keyExtractor(item) {
    return item.uid;
  }
  renderItem({ item }) {
    return <ListEmployee employee={item} navigation={this.props.navigation} />;
  }
  render() {
    return (
      <FlatList
      data={this.props.employees}
      renderItem={this.renderItem.bind(this)} // Only for test
      keyExtractor={this.keyExtractor}
      navigation={this.props.navigation}
      />
    );
  }
}
const mapStateToProps = (state) => {
  const employees = _.map(state.employees, (val, uid) => ({ ...val, uid }));
  return { employees };
};

export default connect(mapStateToProps, { employeesFetch })(EmployeeList);

ListEmployee.js

import React, { Component } from 'react';
import {
  Text,
  StyleSheet,
  TouchableWithoutFeedback,
  View
} from 'react-native';
import { CardSection } from './common';

class ListEmployee extends Component {

  render() {
  const { employee } = this.props;
  const { navigate } = this.props.navigation;
  const { textStyle } = styles;
  const { name } = this.props.employee;
    return (
      <TouchableWithoutFeedback onPress={() => navigate('employeeEdit', { employee })}>
        <View>
          <CardSection>
            <Text style={textStyle}>{name}</Text>
          </CardSection>
        </View>
      </TouchableWithoutFeedback>
    );
  }
}

/**
 second argument in connect does 2 things. 1.  dispatches all actions creators
return action objects to the store to be used by reducers; 2. creates props
of action creators to be used by components
**/
export default ListEmployee;

const styles = StyleSheet.create({
  textStyle: {
    fontSize: 18,
    paddingLeft: 15,
  }
});

EmployeeForm.js

import React, { Component } from 'react';
import { Picker, Text, StyleSheet, View } from 'react-native';
import { connect } from 'react-redux';
import { employeeUpdate } from '../actions';
import { CardSection, Input } from './common';

class EmployeeForm extends Component {
    render() {
        return (
        <View>
          <CardSection>
            <Input
              label="Name"
              placeholder="Jane"
              value={this.props.name}
              onChangeText={value => this.props.employeeUpdate({ prop: 'name', value })}
            />
          </CardSection>

          <CardSection>
          <Input
            label="Phone"
            placeholder="xxx-xxxx"
            value={this.props.phone}
            onChangeText={value => this.props.employeeUpdate({ prop: 'phone', value })}
          />
          </CardSection>

          <CardSection>
          <Text style={styles.pickerTextStyle}>Shift</Text>
          <Picker
            style={{ flex: 1 }}
            selectedValue={this.props.shift}
            onValueChange={value => this.props.employeeUpdate({ prop: 'shift', value })}
          >
            <Picker.Item label="Monday" value="Monday" />
            <Picker.Item label="Tuesday" value="Tuesday" />
            <Picker.Item label="Wednesday" value="Wednesday" />
            <Picker.Item label="Thursday" value="Thursday" />
            <Picker.Item label="Friday" value="Friday" />
            <Picker.Item label="Saturday" value="Saturday" />
            <Picker.Item label="Sunday" value="Sunday" />
          </Picker>
          </CardSection>
        </View>
      );
    }
  }

  const styles = StyleSheet.create({
    pickerTextStyle: {
      fontSize: 18,
      paddingLeft: 20
    }
  });

  const mapStateToProps = (state) => {
    const { name, phone, shift } = state.EmployeeForm;

    return { name, phone, shift };
  };

export default connect(mapStateToProps, { employeeUpdate })(EmployeeForm);

这是我的combineReducers

import { combineReducers } from 'redux';
import AuthReducer from './AuthReducer';
import EmployeeFormReducer from './EmployeeFormReducer';
import EmployeeReducer from './EmployeeReducer';
import SelectionReducer from './SelectionReducer';

export default combineReducers({
    auth: AuthReducer,
    employeeForm: EmployeeFormReducer,
    employees: EmployeeReducer,
    selectedEmployeeId: SelectionReducer
  });

这里是EmployeeFormReducer

import { EMPLOYEE_UPDATE, EMPLOYEE_CREATE } from '../actions/types';

const INITIAL_STATE = {
  // prop: 'name', value: ''
  name: '',
  phone: '',
  shift: ''
};

export default (state = INITIAL_STATE, action) => {
    switch (action.type) {
      case EMPLOYEE_UPDATE:

// [] is NOT an array; a syntax to convert value into key; called key interpolation??
        return { ...state, [action.payload.prop]: action.payload.value };
      case EMPLOYEE_CREATE:
        return INITIAL_STATE;
      default:
        return state;
    }
};

这里是行动创作者

export const employeeUpdate = ({ prop, value }) => {
  return {
    type: EMPLOYEE_UPDATE,
    payload: { prop, value }
  };
};

这是EmployeeForm.js

import React, { Component } from 'react';
import { Picker, Text, StyleSheet, View } from 'react-native';
import { connect } from 'react-redux';
import { employeeUpdate } from '../actions';
import { CardSection, Input } from './common';

class EmployeeForm extends Component {
    render() {
        return (
        <View>
          <CardSection>
            <Input
              label="Name"
              placeholder="Jane"
              value={this.props.name}
              onChangeText={value => this.props.employeeUpdate({ prop: 'name', value })}
            />
          </CardSection>

          <CardSection>
          <Input
            label="Phone"
            placeholder="xxx-xxxx"
            value={this.props.phone}
            onChangeText={value => this.props.employeeUpdate({ prop: 'phone', value })}
          />
          </CardSection>

          <CardSection>
          <Text style={styles.pickerTextStyle}>Shift</Text>
          <Picker
            style={{ flex: 1 }}
            selectedValue={this.props.shift}
            onValueChange={value => this.props.employeeUpdate({ prop: 'shift', value })}
          >
            <Picker.Item label="Monday" value="Monday" />
            <Picker.Item label="Tuesday" value="Tuesday" />
            <Picker.Item label="Wednesday" value="Wednesday" />
            <Picker.Item label="Thursday" value="Thursday" />
            <Picker.Item label="Friday" value="Friday" />
            <Picker.Item label="Saturday" value="Saturday" />
            <Picker.Item label="Sunday" value="Sunday" />
          </Picker>
          </CardSection>
        </View>
      );
    }
  }

  const styles = StyleSheet.create({
    pickerTextStyle: {
      fontSize: 18,
      paddingLeft: 20
    }
  });

  const mapStateToProps = (state) => {
    const { name, phone, shift } = state.EmployeeForm;

    return { name, phone, shift };
  };

export default connect(mapStateToProps, { employeeUpdate })(EmployeeForm);

2 个答案:

答案 0 :(得分:1)

我认为你的问题是你没有约束力。试试这个:

onButtonPress = () => {
    const { params } = this.props.navigation.state;
    const { name, phone, shift } = params.employee;
  }

答案 1 :(得分:0)

问题是EmployeeForm.js;这是EmployeeEdit.js和EmployeeForm.js之间共享的通用组件。它从2中的任何一个接收,但它必须没有mapStateToProps,因为它最终会覆盖负责管理redux状态的父级mapStateToProps。所以我摆脱了它,combinereducers能够将state.key与其相应的值匹配,即在函数调用中声明的reducers。