所有
我遇到了一个在函数中未定义的this.props的问题。我想我忘记了某个地方,但我找不到。
在我的“页面”中:
进口:
import React, { Component } from 'react';'''''
import { View, Text, Image } from 'react-native';
import { Button } from '../components';
import { Container, Content, Item, Input } from 'native-base';
import { connect } from 'react-redux';
import {increment, decrement } from '../actions';
功能声明:
class EltTestPage extends Component {
constructor(props) {
super(props);
}
increment(){
console.log("DEBUG : props in function :"+this.props);
this.props.increment();
}
decrement(){
this.props.decrement();
}
渲染:
render() {
const {elt} = this.props;
console.log("DEBUG : props in render :"+this.props);
return (
<Container>
<Content>
<View>
<Item>
<Button
onPress={this.increment}
style={{width: 30, height: 30}}>
<Text>+</Text>
</Button>
{//many other things here}
</Item>
</View>
</Content>
</Container>
);
}
连接
export default connect(mapStateToProps, {increment,decrement } ) ( EltTestPage );
在我的操作文件中:
import { Actions } from 'react-native-router-flux';
import {
INCREMENT_NBRE,
DECREMENT_NBRE
} from './types';
export const increment=()=>{
return {
type: INCREMENT_NBRE,
};
}
export const decrement=()=> {
return {
type: DECREMENT_NBRE,
};
}
在Actions / index.js中添加
console.log中的结果
03-29 10:21:42.613 4210-6713/? I/ReactNativeJS: DEBUG : props in render :
03-29 10:21:42.613 4210-6713/? I/ReactNativeJS: { screenProps: undefined,
navigation:
{ dispatch: [Function],
state:
{ routeName: 'eltTest',
key: 'Init-id-1522318902557-0',
params:
{ title: 'composant',
hideNavBar: true,
panHandlers: null,
init: true } },
goBack: [Function: goBack],
navigate: [Function: navigate],
setParams: [Function: setParams] },
title: 'composant',
hideNavBar: true,
panHandlers: null,
init: true,
name: 'eltTest',
elt: { nbre: 20, nbreTxt: '20' },
increment: [Function],
decrement: [Function] }
03-29 10:21:43.749 4210-6713/? I/ReactNativeJS: DEBUG : props in function :
03-29 10:21:43.749 4210-6713/? I/ReactNativeJS: undefined
03-29 10:21:43.750 4210-6713/? E/ReactNativeJS: undefined is not an object (evaluating 'this.props.increment')