2017-09-13 1 views
1
내 등록 페이지에서 돌아 오는을 사용하려면

그래서 사용자 감속기 생성 : 다음과 같이 생성됩니다돌아 오는 mapStateToProps이 제대로 연결되지

const user = (state = initialState, action) => { 
    switch (action.type) { 
     case 'TYPE_FIRSTNAME': 
      console.log('typed first name ' + action.text); 
      return { ...state, firstName: action.text }; 
     case 'TYPE_LASTNAME': 
      return { ...state, lastName: action.text }; 
     case 'TYPE_EMAIL': 
      return { ...state, email: action.text }; 
     case 'TYPE_PASSWORD': 
      return { ...state, password: action.text }; 
     default: 
      return state; 
    } 
} 

을 :

const AppReducer = combineReducers({ 
     nav, 
     user 
    }); 
export default AppReducer; 

탐색 감속기입니다 네비게이션 (반응 네비게이션과 함께 사용하면 잘 작동합니다). 그 다음에 컨테이너를 만들었습니다.

const mapStateToProps = state => { 
    return { 
     firstName: state.firstName, 
     lastName: state.lastName, 
    } 
}; 
const mapDispatchToProps = (dispatch, ownProps) => ({ 
    typeFirstName: (text) => {console.log('typed firstname'); 
    dispatch({type: 'TYPE_FIRSTNAME', text})}, 
    typeLastName: (text) => dispatch({type: 'TYPE_LASTNAME', text}), 
    registerUser:() => { 
     //register("mamad"); 
     console.log('called register user : ');   
     dispatch({type: 'MAINSCREEN'}) 
    } 
}); 

export default connect(mapStateToProps, 
    mapDispatchToProps)(RegisterScene) 

하지만 결코 호출되지 않습니다. 이유는 무엇입니까?

+0

'mapStateToProps'의'return {'줄에 디버거를두면 절대 적중하지 않는다고 말하는 겁니까? –

+0

결코 부르지 않는 것은 무엇입니까? 'typeFirstName'과 같은 액션 크리에이터 중 한 명을 호출하는 경우 코드 –

+0

을 codeandbox.io – Deano

답변

2

유일한 문제는 mapStateToProps입니다. 나는 그것이라고 생각한다

const mapStateToProps = state => { 
    return { 
     firstName: state.user.firstName, 
     lastName: state.user.lastName, 
    } 
}; 

여기에 오류 로그를 입력하면 도움이 될 것입니다.

2

축소 기 (reducer)를 결합하면 상태가 지정된 상태 분기에 놓입니다.

귀하의 경우에는 state.user이 필요합니다.

그래서 당신의 mapStateToProps 기능과 같이 보일 것입니다 : 내가 찾은

const mapStateToProps = state => { 
    return { 
     firstName: state.user.firstName, 
     lastName: state.user.lastName, 
    } 
}; 
+0

덕분에 지금은 전혀 작동하지 않는다. – Mamadou