score:0

Accepted answer

thought id post my solution as an answer as it combined parts of both answers, but needed bits of both to fully solve the issue.

by combining parts of both the above answers i was able to fully solve the problem. i removed getstate() as both fctmolina and rodrigo amaral suggested. i also simplified the action creator to returning a javascript object, rather than a function, and so no longer needed to include a dispatch function, or use redux-thunk. i passed the newcomment variable into my action creator, and then combined it with my old state inside the reducer. the solution only required a simply definition of the mapdispatchtoprops as a js object containing the action creator getvideocomments, which made it available as a prop to commentsection, and resulted in the action creator being dispatched when the this.props.getvideocomments() function call was made. here is the altered code:

commentsection.js

import react from 'react'
import commentlist from './commentlist'
import commentform from './commentform'


//redux stuff
import {connect} from 'react-redux'
import {getvideocomments} from '../actions'

class commentssection extends react.component{

    constructor(props){
        super(props)
        //this.state={comments:[], loading:false}

    }

    componentdidmount(){
        console.log(this.props.comments)
    }


    handlecommentsubmit = (newcomment) =>{
        // call action creator to dist action to all reducers and update relevant states
        this.props.getvideocomments(newcomment)

    }
    //comments are create in comment form, passed up then sent down through commentlist to individual comment rendering inside comment.js
// comment form oncommentsubmit running everytime it renders, not only on submital
    render(){
        const {comments} = this.props
        console.log({comments})
        return(
            <div>
                <span><h4> comments </h4></span>
                <div classname="ui grid"> 


                    <div classname = "right floated eight wide column" >
                        <commentlist comments={comments}/> 
                    </div>
                    <div classname="left floated eight wide column">

                        <commentform oncommentsubmit={this.handlecommentsubmit}/>

                    </div>
                 </div>
             </div>

        )

    }
}

//redux stuff
//called following state update
const mapstatetoprops = (state) => {

    return {comments:state.videocomments}
}

export default connect(mapstatetoprops,{getvideocomments:getvideocomments})(commentssection)

videocommentsreducer.js

import react from 'react'

 const videocommentsreducer=function(state= [], action){ // reads in previous state
    switch (action.type){
        case 'get_video_comments':
            return [...state, action.payload] //reducer will update state to be payload.videocomments. action only describes what happened
                                         // reducer describes how what happened effects state. could also use previous state and action to create new data
        default: 
            return state
    }
}

export default videocommentsreducer

index.js (for action creator)

import react from 'react'

export const getvideocomments = (newcomment) => {
    return({
        type: 'get_video_comments',
        payload: newcomment
    })
};

score:1

from your action creator file, it seems that you are using the redux-thunk middleware, so make sure to import this library and apply it in the store. this codesandbox shows a complete example based on yours.

when using this thunk, make sure to always use the dispatch that it provides in order to send the action to the store. don't return an object from the bound action creator:

export const getvideocomments = () => {
  return (dispatch, getstate) => {
    const videocomments = getrandomcomments();

    dispatch({
      type: "get_video_comments",
      payload: videocomments
    });
  };
};

also, it doesn't make sense to use getstate here to get the video comments. you would simply update the store with the same state over and over again. getstate is useful when you want to interact with a different part of the state, that is outside the reducer that captures your action type.

score:1

use mapdispatchtoprops in your commentsection.js and there's no need to use getstate in your action creator.

action creator

const getvideocomments = (comments) => ({
   type: 'get_video_comments',
   payload: comments,
});

commentsection.js

// handlecommentsubmit
handlecommentsubmit = (newcomment) => {
   this.props.getvideocomments(newcomment); //pass comment to action then access newcomment in reducer then add it to your state
}

mapdispatchtoprops = (state) => {
   getvideocomments: (newcomment) => dispatch(getvideocomments(newcomment)),
}

export default connect(mapstatetoprops, mapdispatchtoprops)(commentssection);


reducer.js

case 'get_video_comments':
   return [...state, action.payload];

Related Query

More Query from same tag