2017-12-21 18 views
2

これはそれぞれのアクションを呼び出すメインファイルです - addPrescriptionしかしthis.props.prescriptionにアクセスしているときに理想的に必要なデータが含まれている必要があります - 私はcomponentWillReceivePropsにアクセスできません。componentWillReceivePropsはアクションにもリダンダントが呼び出されません

import {connect} from 'react-redux'; 
import { addPrescription } from '../../actions/index'; 

class Prescription extends Component{ 
constructor(props) { 
super(props); 
this.state = {editorState: EditorState.createEmpty()}; 
this.onChange = (editorState) => this.setState({editorState}); 
} 

createPrescription(prescription){ 
     this.props.addPrescription("pravandan"); 
     console.log(this.state.prescription); 
} 

componentWillReceiveProps(nextProps){ 
    console.log(nextProps.prescription) 
} 

render(){ 
    return(
      <div> 
       <br/> 
       <br/> 
       <Row> 
        <Col span={12} push={6}> 
         <Button type="primary" onClick={() => this.createPrescription("")}>Submit</Button> 
         <br/> 
         <Button type="primary" onClick={() => this.createPrescription("")}>{this.props.prescription}</Button> 
        </Col> 
       </Row> 
      </div> 
     ); 
} 
} 

const mapStateToProps = (state, ownProps) => { 
return { 
prescription : state.prescription 
} 
}; 

const mapDispatchToProps = (dispatch) => { 
return { 
addPrescription : prescription => dispatch(addPrescription(prescription)) 
} 
}; 

export default connect(mapStateToProps, mapDispatchToProps)(Prescription); 
+0

「componentWillReceiveProps'にアクセスできません」という意味はどうですか? Reactは、btwのマウント中にinitialpropsでcomponentWillReceiveProps()を呼び出さない。これについて[こちら](https://reactjs.org/docs/react-component.html#componentwillreceiveprops)を読むことができます。 –

+0

@ KyleRichardsonええ、私は、この関数は、アクションが発生した後に呼び出される必要があることを意味した、na –

+0

私はあなたのレデューサーをご覧くださいありますか? –

答えて

1

あなたがこの投稿減速によると:

const addprescription = (state=[],action) => { 
     console.log("reducers received") 

    switch(action.type) { 
     case 'ADD_PRESCRIPTION' : 
      return [ 
       ...state, 
       { 
        prescription : action.prescription 
       } 
      ] 

     default : 
      return state; 
    } 
} 

export default addprescription; 

をお使いの減速がaddprescriptionという名前です。では、あなたのmapStateToPropsあなたの減速の名前を変更

prescription : state.prescription

prescription : state.addprescription

いっそ

変更...にする必要があります。現在、state.prescriptionは存在しません。 state.addprescription[0].prescriptionはあなたのレデューサーが行動を取った後に存在するものですが、あなたはレデューサー名のためにこの状態をあなたの小道具にマッピングしていません。

EDIT:コードサンドボックスを見た後。

state.addprescription[0].prescriptionmapStateToPropsに追加しました。問題は、あなたの初期状態は空の配列なので、エラーがどのようにスローされるのかがわかります。

const mapStateToProps = (state, ownProps) => { 
    return { 
     prescription : state.addprescription[0].prescription 
    } 
    console.log(state.prescription) 
}; 

からstate.addprescriptionに変更してください。

ここで、addprescriptionアレイにオブジェクトを追加しています。あなたのレデューサーのオブジェクトに追加するprescriptionキーは、this.props.prescription[index].prescriptionで利用できるようになります。あなたの命名規則を再考したいかもしれませんか?

ここは裸の骨working code sandbox exampleです。トップダウンから簡単にフォローできます。

+0

ありがとうございます。しかし、このエラーが発生しています。TypeError:未定義のプロパティ '0'を読み取ることができません。 –

+0

あなたのレデューサー、コンポーネント、アクション作成者の[コードサンドボックス](https://codesandbox.io/s/new)を作ってください。完全なモジュールをデバッグする方がはるかに簡単です。 –

+0

ここにあります - https://codesandbox.io/s/github/Pravandan/docspad/tree/master/ –

関連する問題