Updating React Component with multiple Redux actions










1















So I've been bashing my brain against this problem for awhile. Its a react app with redux state management. The problem is the component will not update when a click event is used to call a action. I'm rendering the button dynamically based on a certain prop in the state, if the button is clicked, it triggers a function that calls a prop which dispatches the action. Below is the code.



Here is the component:



import React, Component from 'react';
import moment from 'moment';
import Table from 'antd';
import connect from "react-redux";
import * as rxActions from '../actions/rxActions';

const columns = [

title: 'Prescriber', dataIndex: 'doctor', rowKey: 'doctor'
,

title: 'Submitted', dataIndex: 'timestamp', rowKey: 'timestamp'
,

title: 'Prescription', dataIndex: 'prescription', rowKey: 'prescription'
,

title: 'Refills', dataIndex: 'refills', rowKey: 'refills'
,

title: 'Qty', dataIndex: 'quantity', rowKey: 'quantity'
,

title: 'Exp', dataIndex: 'expDate', rowKey: 'exp'
,

title: 'Status', dataIndex: 'status', rowKey: 'status'

];

class RxHistory extends Component

handleFill(data)
this.props.fetchFillRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"pharmacist": "pha",
"phLicense": "pha01",
"prescription": data.prescription,
"refills": data.refills,
"status": 'filled',
"expDate": moment(data.expDate).valueOf()

)
;

handleApprove(data)
this.props.fetchApproveRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"approved": "true"

)

;

checkStatus(data)
if (this.props.provider.type === 'pharmacist')
if (data.status !== 'filled')
return (
data.status = <button
type='button'
onClick=() =>
this.handleFill(data)
>
Fill Rx
</button>
)

return (data.status = 'filled')

if (this.props.provider.type === 'insurance')
if (data.status === 'filled')
if (data.approved === 'false')
return (
data.status = <button
type='button'
onClick=() => this.handleApprove(data)>
Approve Rx
</button>
)



return data.status
;

renderTable()
if (this.props.rxHistory.isFetching)
return (
<div className='ant-table-placeholder'>Loading...</div>
)

if (this.props.rxHistory.rx.rxList)
const rxHistory = this.props.rxHistory.rx.rxList
.map(data =>
data.timestamp = moment(data.timestamp).format('MM/DD/YYYY');
data.expDate = moment(data.expDate).format('MM/DD/YYYY');
data.status = this.checkStatus(data);
return data
);
if (this.props.provider.type !== 'doctor')
const reducedHistory = rxHistory.reduce((acc, data) =>
if (data.status !== 'filled' && data.approved !== 'true')
acc.push(data);

return acc;
, );

return (
<Table columns=columns dataSource=reducedHistory rowKey=reducedHistory => reducedHistory.rxid/>
);


return (
<Table columns=columns dataSource=rxHistory rowKey=rxHistory => rxHistory.rxid/>
);


return(
<Table columns=columns rowKey=null />
)



render()
return (
this.renderTable()
)




const mapStateToProps = ( onePatient, rxHistory ) =>
return onePatient, rxHistory
;

export default connect(mapStateToProps, rxActions)(RxHistory);


Here is the Action:



export const fetchFillRx = data => async dispatch => 
dispatch(type: FILLING_RX);
try
const res = await axios.patch(rxUrl, data);
dispatch(
type: FILL_RX,
payload: res.data
).then(
dispatch(type: FETCH_RX_HISTORY)
)

catch (e)
dispatch(
type: FILL_RX_ERROR,
payload: e
)

;


Here is the reducer:



import 
FILLING_RX,
FILL_RX,
FILL_RX_ERROR
from "../actions/types";

const INITIAL_STATE =
isFetching: false,
res: ""
;

export default (state = INITIAL_STATE, action) =>
switch (action.type)
case FILLING_RX:
return
...state,
isFetching: true,
;

case FILL_RX:
return
...state,
isFetching: false,
res: action.payload,
;

case FILL_RX_ERROR:
return
...state,
isFetching: false,
res: "Failed to fill Rx",
;

default:
return
...state
;

;


The effects I've been experiencing is either an infinite loop when trying to use componentDidUpdate, or it will process the action, display the action in redux dev tools, but not update the component, or will say that the property of rxHistory.rx.rxList is undefined.



Any help would be greatly appreciated.










share|improve this question






















  • So I figured out that it has to do with a limitation of shallow comparisons made by redux. To solve the problem, I'd have to either write a very convoluted method of reducing each reducer to flatten the object, or alter that data structure to not have as many nested objects. The other solution would be to use a helper package that helps standardize the data.

    – Chris K.
    Nov 29 '18 at 1:19















1















So I've been bashing my brain against this problem for awhile. Its a react app with redux state management. The problem is the component will not update when a click event is used to call a action. I'm rendering the button dynamically based on a certain prop in the state, if the button is clicked, it triggers a function that calls a prop which dispatches the action. Below is the code.



Here is the component:



import React, Component from 'react';
import moment from 'moment';
import Table from 'antd';
import connect from "react-redux";
import * as rxActions from '../actions/rxActions';

const columns = [

title: 'Prescriber', dataIndex: 'doctor', rowKey: 'doctor'
,

title: 'Submitted', dataIndex: 'timestamp', rowKey: 'timestamp'
,

title: 'Prescription', dataIndex: 'prescription', rowKey: 'prescription'
,

title: 'Refills', dataIndex: 'refills', rowKey: 'refills'
,

title: 'Qty', dataIndex: 'quantity', rowKey: 'quantity'
,

title: 'Exp', dataIndex: 'expDate', rowKey: 'exp'
,

title: 'Status', dataIndex: 'status', rowKey: 'status'

];

class RxHistory extends Component

handleFill(data)
this.props.fetchFillRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"pharmacist": "pha",
"phLicense": "pha01",
"prescription": data.prescription,
"refills": data.refills,
"status": 'filled',
"expDate": moment(data.expDate).valueOf()

)
;

handleApprove(data)
this.props.fetchApproveRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"approved": "true"

)

;

checkStatus(data)
if (this.props.provider.type === 'pharmacist')
if (data.status !== 'filled')
return (
data.status = <button
type='button'
onClick=() =>
this.handleFill(data)
>
Fill Rx
</button>
)

return (data.status = 'filled')

if (this.props.provider.type === 'insurance')
if (data.status === 'filled')
if (data.approved === 'false')
return (
data.status = <button
type='button'
onClick=() => this.handleApprove(data)>
Approve Rx
</button>
)



return data.status
;

renderTable()
if (this.props.rxHistory.isFetching)
return (
<div className='ant-table-placeholder'>Loading...</div>
)

if (this.props.rxHistory.rx.rxList)
const rxHistory = this.props.rxHistory.rx.rxList
.map(data =>
data.timestamp = moment(data.timestamp).format('MM/DD/YYYY');
data.expDate = moment(data.expDate).format('MM/DD/YYYY');
data.status = this.checkStatus(data);
return data
);
if (this.props.provider.type !== 'doctor')
const reducedHistory = rxHistory.reduce((acc, data) =>
if (data.status !== 'filled' && data.approved !== 'true')
acc.push(data);

return acc;
, );

return (
<Table columns=columns dataSource=reducedHistory rowKey=reducedHistory => reducedHistory.rxid/>
);


return (
<Table columns=columns dataSource=rxHistory rowKey=rxHistory => rxHistory.rxid/>
);


return(
<Table columns=columns rowKey=null />
)



render()
return (
this.renderTable()
)




const mapStateToProps = ( onePatient, rxHistory ) =>
return onePatient, rxHistory
;

export default connect(mapStateToProps, rxActions)(RxHistory);


Here is the Action:



export const fetchFillRx = data => async dispatch => 
dispatch(type: FILLING_RX);
try
const res = await axios.patch(rxUrl, data);
dispatch(
type: FILL_RX,
payload: res.data
).then(
dispatch(type: FETCH_RX_HISTORY)
)

catch (e)
dispatch(
type: FILL_RX_ERROR,
payload: e
)

;


Here is the reducer:



import 
FILLING_RX,
FILL_RX,
FILL_RX_ERROR
from "../actions/types";

const INITIAL_STATE =
isFetching: false,
res: ""
;

export default (state = INITIAL_STATE, action) =>
switch (action.type)
case FILLING_RX:
return
...state,
isFetching: true,
;

case FILL_RX:
return
...state,
isFetching: false,
res: action.payload,
;

case FILL_RX_ERROR:
return
...state,
isFetching: false,
res: "Failed to fill Rx",
;

default:
return
...state
;

;


The effects I've been experiencing is either an infinite loop when trying to use componentDidUpdate, or it will process the action, display the action in redux dev tools, but not update the component, or will say that the property of rxHistory.rx.rxList is undefined.



Any help would be greatly appreciated.










share|improve this question






















  • So I figured out that it has to do with a limitation of shallow comparisons made by redux. To solve the problem, I'd have to either write a very convoluted method of reducing each reducer to flatten the object, or alter that data structure to not have as many nested objects. The other solution would be to use a helper package that helps standardize the data.

    – Chris K.
    Nov 29 '18 at 1:19













1












1








1








So I've been bashing my brain against this problem for awhile. Its a react app with redux state management. The problem is the component will not update when a click event is used to call a action. I'm rendering the button dynamically based on a certain prop in the state, if the button is clicked, it triggers a function that calls a prop which dispatches the action. Below is the code.



Here is the component:



import React, Component from 'react';
import moment from 'moment';
import Table from 'antd';
import connect from "react-redux";
import * as rxActions from '../actions/rxActions';

const columns = [

title: 'Prescriber', dataIndex: 'doctor', rowKey: 'doctor'
,

title: 'Submitted', dataIndex: 'timestamp', rowKey: 'timestamp'
,

title: 'Prescription', dataIndex: 'prescription', rowKey: 'prescription'
,

title: 'Refills', dataIndex: 'refills', rowKey: 'refills'
,

title: 'Qty', dataIndex: 'quantity', rowKey: 'quantity'
,

title: 'Exp', dataIndex: 'expDate', rowKey: 'exp'
,

title: 'Status', dataIndex: 'status', rowKey: 'status'

];

class RxHistory extends Component

handleFill(data)
this.props.fetchFillRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"pharmacist": "pha",
"phLicense": "pha01",
"prescription": data.prescription,
"refills": data.refills,
"status": 'filled',
"expDate": moment(data.expDate).valueOf()

)
;

handleApprove(data)
this.props.fetchApproveRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"approved": "true"

)

;

checkStatus(data)
if (this.props.provider.type === 'pharmacist')
if (data.status !== 'filled')
return (
data.status = <button
type='button'
onClick=() =>
this.handleFill(data)
>
Fill Rx
</button>
)

return (data.status = 'filled')

if (this.props.provider.type === 'insurance')
if (data.status === 'filled')
if (data.approved === 'false')
return (
data.status = <button
type='button'
onClick=() => this.handleApprove(data)>
Approve Rx
</button>
)



return data.status
;

renderTable()
if (this.props.rxHistory.isFetching)
return (
<div className='ant-table-placeholder'>Loading...</div>
)

if (this.props.rxHistory.rx.rxList)
const rxHistory = this.props.rxHistory.rx.rxList
.map(data =>
data.timestamp = moment(data.timestamp).format('MM/DD/YYYY');
data.expDate = moment(data.expDate).format('MM/DD/YYYY');
data.status = this.checkStatus(data);
return data
);
if (this.props.provider.type !== 'doctor')
const reducedHistory = rxHistory.reduce((acc, data) =>
if (data.status !== 'filled' && data.approved !== 'true')
acc.push(data);

return acc;
, );

return (
<Table columns=columns dataSource=reducedHistory rowKey=reducedHistory => reducedHistory.rxid/>
);


return (
<Table columns=columns dataSource=rxHistory rowKey=rxHistory => rxHistory.rxid/>
);


return(
<Table columns=columns rowKey=null />
)



render()
return (
this.renderTable()
)




const mapStateToProps = ( onePatient, rxHistory ) =>
return onePatient, rxHistory
;

export default connect(mapStateToProps, rxActions)(RxHistory);


Here is the Action:



export const fetchFillRx = data => async dispatch => 
dispatch(type: FILLING_RX);
try
const res = await axios.patch(rxUrl, data);
dispatch(
type: FILL_RX,
payload: res.data
).then(
dispatch(type: FETCH_RX_HISTORY)
)

catch (e)
dispatch(
type: FILL_RX_ERROR,
payload: e
)

;


Here is the reducer:



import 
FILLING_RX,
FILL_RX,
FILL_RX_ERROR
from "../actions/types";

const INITIAL_STATE =
isFetching: false,
res: ""
;

export default (state = INITIAL_STATE, action) =>
switch (action.type)
case FILLING_RX:
return
...state,
isFetching: true,
;

case FILL_RX:
return
...state,
isFetching: false,
res: action.payload,
;

case FILL_RX_ERROR:
return
...state,
isFetching: false,
res: "Failed to fill Rx",
;

default:
return
...state
;

;


The effects I've been experiencing is either an infinite loop when trying to use componentDidUpdate, or it will process the action, display the action in redux dev tools, but not update the component, or will say that the property of rxHistory.rx.rxList is undefined.



Any help would be greatly appreciated.










share|improve this question














So I've been bashing my brain against this problem for awhile. Its a react app with redux state management. The problem is the component will not update when a click event is used to call a action. I'm rendering the button dynamically based on a certain prop in the state, if the button is clicked, it triggers a function that calls a prop which dispatches the action. Below is the code.



Here is the component:



import React, Component from 'react';
import moment from 'moment';
import Table from 'antd';
import connect from "react-redux";
import * as rxActions from '../actions/rxActions';

const columns = [

title: 'Prescriber', dataIndex: 'doctor', rowKey: 'doctor'
,

title: 'Submitted', dataIndex: 'timestamp', rowKey: 'timestamp'
,

title: 'Prescription', dataIndex: 'prescription', rowKey: 'prescription'
,

title: 'Refills', dataIndex: 'refills', rowKey: 'refills'
,

title: 'Qty', dataIndex: 'quantity', rowKey: 'quantity'
,

title: 'Exp', dataIndex: 'expDate', rowKey: 'exp'
,

title: 'Status', dataIndex: 'status', rowKey: 'status'

];

class RxHistory extends Component

handleFill(data)
this.props.fetchFillRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"pharmacist": "pha",
"phLicense": "pha01",
"prescription": data.prescription,
"refills": data.refills,
"status": 'filled',
"expDate": moment(data.expDate).valueOf()

)
;

handleApprove(data)
this.props.fetchApproveRx(

"patientID": this.props.onePatient.data.patientID,
"rxid": data.rxid,
"timestamp": moment(data.timestamp).valueOf(),
"approved": "true"

)

;

checkStatus(data)
if (this.props.provider.type === 'pharmacist')
if (data.status !== 'filled')
return (
data.status = <button
type='button'
onClick=() =>
this.handleFill(data)
>
Fill Rx
</button>
)

return (data.status = 'filled')

if (this.props.provider.type === 'insurance')
if (data.status === 'filled')
if (data.approved === 'false')
return (
data.status = <button
type='button'
onClick=() => this.handleApprove(data)>
Approve Rx
</button>
)



return data.status
;

renderTable()
if (this.props.rxHistory.isFetching)
return (
<div className='ant-table-placeholder'>Loading...</div>
)

if (this.props.rxHistory.rx.rxList)
const rxHistory = this.props.rxHistory.rx.rxList
.map(data =>
data.timestamp = moment(data.timestamp).format('MM/DD/YYYY');
data.expDate = moment(data.expDate).format('MM/DD/YYYY');
data.status = this.checkStatus(data);
return data
);
if (this.props.provider.type !== 'doctor')
const reducedHistory = rxHistory.reduce((acc, data) =>
if (data.status !== 'filled' && data.approved !== 'true')
acc.push(data);

return acc;
, );

return (
<Table columns=columns dataSource=reducedHistory rowKey=reducedHistory => reducedHistory.rxid/>
);


return (
<Table columns=columns dataSource=rxHistory rowKey=rxHistory => rxHistory.rxid/>
);


return(
<Table columns=columns rowKey=null />
)



render()
return (
this.renderTable()
)




const mapStateToProps = ( onePatient, rxHistory ) =>
return onePatient, rxHistory
;

export default connect(mapStateToProps, rxActions)(RxHistory);


Here is the Action:



export const fetchFillRx = data => async dispatch => 
dispatch(type: FILLING_RX);
try
const res = await axios.patch(rxUrl, data);
dispatch(
type: FILL_RX,
payload: res.data
).then(
dispatch(type: FETCH_RX_HISTORY)
)

catch (e)
dispatch(
type: FILL_RX_ERROR,
payload: e
)

;


Here is the reducer:



import 
FILLING_RX,
FILL_RX,
FILL_RX_ERROR
from "../actions/types";

const INITIAL_STATE =
isFetching: false,
res: ""
;

export default (state = INITIAL_STATE, action) =>
switch (action.type)
case FILLING_RX:
return
...state,
isFetching: true,
;

case FILL_RX:
return
...state,
isFetching: false,
res: action.payload,
;

case FILL_RX_ERROR:
return
...state,
isFetching: false,
res: "Failed to fill Rx",
;

default:
return
...state
;

;


The effects I've been experiencing is either an infinite loop when trying to use componentDidUpdate, or it will process the action, display the action in redux dev tools, but not update the component, or will say that the property of rxHistory.rx.rxList is undefined.



Any help would be greatly appreciated.







javascript reactjs redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 4:22









Chris K.Chris K.

61




61












  • So I figured out that it has to do with a limitation of shallow comparisons made by redux. To solve the problem, I'd have to either write a very convoluted method of reducing each reducer to flatten the object, or alter that data structure to not have as many nested objects. The other solution would be to use a helper package that helps standardize the data.

    – Chris K.
    Nov 29 '18 at 1:19

















  • So I figured out that it has to do with a limitation of shallow comparisons made by redux. To solve the problem, I'd have to either write a very convoluted method of reducing each reducer to flatten the object, or alter that data structure to not have as many nested objects. The other solution would be to use a helper package that helps standardize the data.

    – Chris K.
    Nov 29 '18 at 1:19
















So I figured out that it has to do with a limitation of shallow comparisons made by redux. To solve the problem, I'd have to either write a very convoluted method of reducing each reducer to flatten the object, or alter that data structure to not have as many nested objects. The other solution would be to use a helper package that helps standardize the data.

– Chris K.
Nov 29 '18 at 1:19





So I figured out that it has to do with a limitation of shallow comparisons made by redux. To solve the problem, I'd have to either write a very convoluted method of reducing each reducer to flatten the object, or alter that data structure to not have as many nested objects. The other solution would be to use a helper package that helps standardize the data.

– Chris K.
Nov 29 '18 at 1:19












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312400%2fupdating-react-component-with-multiple-redux-actions%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312400%2fupdating-react-component-with-multiple-redux-actions%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線