score:0
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];
Source: stackoverflow.com
Related Query
- How to update redux state using a react variable passed up to the component from a child
- react (class based) : How can I update JSON object in parent component using setState after the object being passed in function from child to parent?
- How to update the state of a sibling component from another sibling or imported component in REACT
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How to update the correct state value in a parent component that gets its value from a child component using hooks in react?
- How to update the global state using redux and remove an item from the global state
- How do I stop state store data from accumulating in a redux component every time I navigate to it using react router
- update react child component's state using props received from an async api call made in the parent component
- How do I make an axios request using a route parameter from React Router, then update state based on the response?
- Is using callback for component state update from redux saga the right way
- How to update the state in functional component using React Hooks
- how does react props merge state passed from parent component and redux store
- How to set state at the server for data from API using react redux
- How to set state in parent from one child component and access the state in another child component using react and typescript?
- How to update the redux state using local state in a component
- How can I update State in React using the React-Select Component and User Selection?
- How can I update react app that gets it's data from the node mongo backend using redux
- React Native - How to pass the Image from the other Component without using React Redux
- How to initialize the react functional component state from props
- How to pass the match when using render in Route component from react router (v4)
- how and when to call a react component methods after state change from redux
- How do I sync state from the DOM to my React Component in an Isomorphic Application?
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- Using React how do I toggle the visibility of a nested component from a container component?
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- How to set state from props that is passed to the component in react?
- How to use useEffect hook properly with array dependency. I passed state from redux store and still my component renders infinitely
- How to wait for a Redux action to change state from a React component
- How to update select items based on the selection from the first select items using react
More Query from same tag
- How to add background image on a material ui Dialog component
- ExpressJS + ReactJS SPA - Different CSRF token generated in every request
- POST API request in React js Form?
- How to avoid Material UI Select focus when option is chosen?
- How can I cancel a state change from a resolving promise after component is already unmounted?
- React Mock Testing useNavigate
- React - useEffect - Undefined variable inside useEffect
- Apply framer-motion to non-functional components
- How to use a type from React for props of my own component?
- Link changing URL but not the page
- react-text-mask how to pass pattern in function react using material-ui form
- How to do the comparison using if inside the render function
- How to fix an error with shallow Enzyme in testing ReactJS App
- React conditional rendering for nested if condition
- How to load third party script and initialise class in React component
- Data in array items not being displayed on the screen in React
- How to mock store in redux toolkit
- Component crashes when re-render | React, Redux
- Open Select after a click on label
- abort http request on change reactjs
- react app hosted in s3 doesn't update after deploy
- Moving fixed content below and above a 100vh component
- React inline styling
- execute function when takeUntil operation is triggered
- ReactJs: callback function is not called in case of this.setState
- How to solve "Element type is invalid" Error?
- React: FileReader onloadend doesn't recognize "this" if defined in a component method
- How to apply custom CSS in material table in react?
- React Hook useEffect has a missing dependency for the setter of useState in React
- Find the time duration/length of an audio file uploaded to a React application