React Redux: how to update vote counts in React Redux












-1















React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered



here



The code below was designed to update a voting system. It works fine by displaying the results as the page loads.



Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.



However, when I click either Upvote por down vote button am having error below in my Reducer



Uncaught (in promise) ReferenceError: items1 is not defined.


This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST



In the backend, I have php code which returns the array data as per below.



Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?



<?php
// Update user response on a post

$return_arr= array("upvote"=>"11", "downvote"=>"7");

echo json_encode($return_arr);
exit;

?>


Here is the array return by API Call



[{"upvote":"11", "downvote":"7"}]


Here is the code



import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';

import { vActions } from '../_actions';

class VoteApp extends React.Component {

constructor(props) {
super(props);
this.state = {
us: 0

};

}

componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}


handledownvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}

render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">

<p>Vote System in React Redux</p>


{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})

<br />

<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />


</li>
)}

</ul>
}


</div>



);
}
}


function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}

const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };


Here is my Reducer



import { userConstants } from '../_constants';

export function posts1(state = {}, action) {
switch (action.type) {



case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};

case userConstants.GETALL_FAILURE:
return {
error: action.error
};



// Reducer section for Sending upvote and down vote

case userConstants.VOTE_REQUEST_POST:

return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)

};

case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {

//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };

}

//return post1;
})
};


case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};




default:
return state
}
}









share|improve this question

























  • I think you should fix the format of the codes first, they can't be read.

    – Marson Mao
    Dec 18 '18 at 3:47
















-1















React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered



here



The code below was designed to update a voting system. It works fine by displaying the results as the page loads.



Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.



However, when I click either Upvote por down vote button am having error below in my Reducer



Uncaught (in promise) ReferenceError: items1 is not defined.


This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST



In the backend, I have php code which returns the array data as per below.



Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?



<?php
// Update user response on a post

$return_arr= array("upvote"=>"11", "downvote"=>"7");

echo json_encode($return_arr);
exit;

?>


Here is the array return by API Call



[{"upvote":"11", "downvote":"7"}]


Here is the code



import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';

import { vActions } from '../_actions';

class VoteApp extends React.Component {

constructor(props) {
super(props);
this.state = {
us: 0

};

}

componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}


handledownvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}

render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">

<p>Vote System in React Redux</p>


{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})

<br />

<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />


</li>
)}

</ul>
}


</div>



);
}
}


function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}

const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };


Here is my Reducer



import { userConstants } from '../_constants';

export function posts1(state = {}, action) {
switch (action.type) {



case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};

case userConstants.GETALL_FAILURE:
return {
error: action.error
};



// Reducer section for Sending upvote and down vote

case userConstants.VOTE_REQUEST_POST:

return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)

};

case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {

//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };

}

//return post1;
})
};


case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};




default:
return state
}
}









share|improve this question

























  • I think you should fix the format of the codes first, they can't be read.

    – Marson Mao
    Dec 18 '18 at 3:47














-1












-1








-1








React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered



here



The code below was designed to update a voting system. It works fine by displaying the results as the page loads.



Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.



However, when I click either Upvote por down vote button am having error below in my Reducer



Uncaught (in promise) ReferenceError: items1 is not defined.


This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST



In the backend, I have php code which returns the array data as per below.



Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?



<?php
// Update user response on a post

$return_arr= array("upvote"=>"11", "downvote"=>"7");

echo json_encode($return_arr);
exit;

?>


Here is the array return by API Call



[{"upvote":"11", "downvote":"7"}]


Here is the code



import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';

import { vActions } from '../_actions';

class VoteApp extends React.Component {

constructor(props) {
super(props);
this.state = {
us: 0

};

}

componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}


handledownvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}

render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">

<p>Vote System in React Redux</p>


{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})

<br />

<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />


</li>
)}

</ul>
}


</div>



);
}
}


function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}

const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };


Here is my Reducer



import { userConstants } from '../_constants';

export function posts1(state = {}, action) {
switch (action.type) {



case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};

case userConstants.GETALL_FAILURE:
return {
error: action.error
};



// Reducer section for Sending upvote and down vote

case userConstants.VOTE_REQUEST_POST:

return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)

};

case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {

//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };

}

//return post1;
})
};


case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};




default:
return state
}
}









share|improve this question
















React Redux: how to update vote counts in React Redux.
A react js equivalents has been answered



here



The code below was designed to update a voting system. It works fine by displaying the results as the page loads.



Here is my problem: I need to update each user's upvote and downvote any time the Upvote Vote and Down Vote button is clicked respectively.



However, when I click either Upvote por down vote button am having error below in my Reducer



Uncaught (in promise) ReferenceError: items1 is not defined.


This error is triggered on
Reducer section for Sending upvote and down vote on constant VOTE_SUCCESS_POST



In the backend, I have php code which returns the array data as per below.



Can someone help me with displaying the array values and updating eg (upvote to 11 and downvote to 7) depending on how the user voted?



<?php
// Update user response on a post

$return_arr= array("upvote"=>"11", "downvote"=>"7");

echo json_encode($return_arr);
exit;

?>


Here is the array return by API Call



[{"upvote":"11", "downvote":"7"}]


Here is the code



import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';

import { vActions } from '../_actions';

class VoteApp extends React.Component {

constructor(props) {
super(props);
this.state = {
us: 0

};

}

componentDidMount() {
this.props.dispatch(userActions.getVote());
}
handleupvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}


handledownvote(person_id,person_vote) {

return (e) => this.props.dispatch(userActions.vote(person_id,person_vote));

}

render() {
const { post1, posts1 } = this.props;
return (
<div style={{background:'red'}} className="well col-md-12">

<p>Vote System in React Redux</p>


{posts1.items1 &&
<ul>
{posts1.items1.map((post1, index1) =>
<li key={post1.id}>
{post1.name} -- (Upvote: {post1.upvote})-- (downvote: {post1.downvote})

<br />

<input type="button" value="upvote" onClick={this.handleupvote(post1.id,1)} />
<input type="button" value="downvote" onClick={this.handledownvote(post1.id,1)} />


</li>
)}

</ul>
}


</div>



);
}
}


function mapStateToProps(state) {
const { posts1} = state;
const { post1 } = state;
return {
post1,
posts1
};
}

const connectedVoteApp = connect(mapStateToProps)(VoteApp);
export { connectedVoteApp as VoteApp };


Here is my Reducer



import { userConstants } from '../_constants';

export function posts1(state = {}, action) {
switch (action.type) {



case userConstants.GETALL_REQUEST:
return {
// ...state,
loading: true
};
case userConstants.GETALL_SUCCESS:
return {
loading: false,
error: null,
items1: action.posts1
};

case userConstants.GETALL_FAILURE:
return {
error: action.error
};



// Reducer section for Sending upvote and down vote

case userConstants.VOTE_REQUEST_POST:

return {
...state,
items1: state.items1.map(post1 =>
post1.id === action.id
? { ...post1}
: post1
)

};

case userConstants.VOTE_SUCCESS_POST:
return {
...state,
items1: state.items1.map(post1 => {
if (post1.id !== action.id) {

//return { ...post1, upvote: action.posts1.items1[0].upvote, downvote: action.posts1.items1[0].downvote };
return { ...post1, upvote: items1[0].upvote, downvote: items1[0].downvote };

}

//return post1;
})
};


case userConstants.VOTE_FAILURE_POST:
return {
error: action.error
};




default:
return state
}
}






reactjs redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 '18 at 20:20







jmarkatti

















asked Dec 18 '18 at 2:42









jmarkattijmarkatti

15219




15219













  • I think you should fix the format of the codes first, they can't be read.

    – Marson Mao
    Dec 18 '18 at 3:47



















  • I think you should fix the format of the codes first, they can't be read.

    – Marson Mao
    Dec 18 '18 at 3:47

















I think you should fix the format of the codes first, they can't be read.

– Marson Mao
Dec 18 '18 at 3:47





I think you should fix the format of the codes first, they can't be read.

– Marson Mao
Dec 18 '18 at 3:47












1 Answer
1






active

oldest

votes


















0














This is what fix my problem. Since the json data returned is in array.
items1: action.posts1[0].upvote



Thanks






share|improve this answer























    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%2f53825713%2freact-redux-how-to-update-vote-counts-in-react-redux%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is what fix my problem. Since the json data returned is in array.
    items1: action.posts1[0].upvote



    Thanks






    share|improve this answer




























      0














      This is what fix my problem. Since the json data returned is in array.
      items1: action.posts1[0].upvote



      Thanks






      share|improve this answer


























        0












        0








        0







        This is what fix my problem. Since the json data returned is in array.
        items1: action.posts1[0].upvote



        Thanks






        share|improve this answer













        This is what fix my problem. Since the json data returned is in array.
        items1: action.posts1[0].upvote



        Thanks







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 11:58









        jmarkattijmarkatti

        15219




        15219






























            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%2f53825713%2freact-redux-how-to-update-vote-counts-in-react-redux%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'