2017-03-15 13 views
0

React와 Redux의 초보자로서 구성 요소에 react-dates을 사용하려고합니다.Redux를 사용하는 구성 요소의 React-Dits

import * as React from 'react'; 
import { connect } from 'react-redux'; 
import { ApplicationState } from '../store'; 
import * as DateState from '../store/Date'; 
import * as SingleDatePicker from 'react-dates'; 

type DateProps = DateState.DateState & typeof DateState.actionCreators; 

class DatePickerSingle extends React.Component<DateProps, any> { 

    public render() { 

     let { date } = this.props; 
     return (
      <div> 
       <SingleDatePicker 
        id="date_input" 
        date={this.props.date} 
        focused={this.state.focused} 
        onDateChange={(date) => { this.props.user({ date }); }} 
        onFocusChange={({ focused }) => { this.setState({ focused }); }} 
        isOutsideRange={() => false} 
        displayFormat="dddd LL"> 
       </SingleDatePicker> 
      </div> 
     ); 
    } 
} 

export default connect(
    (state: ApplicationState) => state.date, 
    DateState.actionCreators     
)(DatePickerSingle); 

이 다음과 같은 오류를 반환 :

이 내 코드입니다

Exception: Call to Node module failed with error: TypeError: Cannot read property 'focused' of null 

focusedonFocusChange는 "날짜 선택기 상태"를 받아야까지 이해한다.

문서는 :

onFocusChange is the callback necessary to update the focus state in the parent component. It expects a single argument of the form { focused: PropTypes.bool }.

그 문제는 내가 약 focused 상태를 모르는 DatePickerSingle 구성 요소에 DateState를 주입 것으로 생각합니다.

내 "고유 한"상태와 DatePicker의 상태를 함께 사용할 수 있습니까? 또는 최선의 접근 방식은 무엇입니까?

저는 지금 꽤 오래 동안 노력하고 있습니다. 누군가가 이것을 도울 수 있기를 바랍니다.

UPDATE

enter image description here

답변

1

대답은 매우 간단합니다 :이 초기화되지 않았기 때문에 this.state가 null입니다. 그냥 props로 구성 요소에 전달됩니다 REDUX에서 오는

constructor() { 
    super(); 
    this.state = { 
    focused: false 
    } 
} 

아무것도를 추가, 당신은뿐만 아니라 구성 요소의 상태를 가질 수 있습니다.

+0

@OlliM! 'Uncaught TypeError :'getHostNode 'of null'을 읽을 수 없습니다. Visual Studio에서'this.setState ({focused})'에 커서를 올리면 여전히 '집중된'은 내 업데이트 된 질문의 스크린 샷을 참조하십시오. – JK87

+0

흠 그 오류가 해결 된 것,하지만 여전히 나는이 오류 : 예외 : 노드 모듈 호출 오류로 실패했습니다 : 불변의 위반 : 요소 형식이 잘못되었습니다 : 예상되는 문자열 (기본 제공 구성 요소) 또는 클래스/함수 (복합 구성 요소의 경우) 있지만 got : object. 'DatePickerSingle'의 렌더링 방법을 확인하십시오. ' – JK87

+0

문제점을 발견했습니다. 'singledatepicker' 컴포넌트의 date 애트리뷰트는 Momentjs [link] (https://momentjs.com/) 객체를 기대하고있었습니다. 내 감속기에서는 이미 형식을 지정 했으므로 문자열이되었습니다. 모든 것이 예상대로 작동합니다. 도와 줘서 고마워! – JK87

관련 문제