I am trying to update my states in redux. But I am clueless on how to do it:
here is the component:
import React from 'react';
import {connect} from 'react-redux';
import fetchTasks from '../actions/getTasks';
class Homepage extends React.Component{
componentDidMount(){
this.props.fetchtsk();
}
render(){
return(
<div>
<h1>Welcome to My Task-Manager Page</h1>
<input type='text' placeholder='insert task'></input>
<button value='insert task'>Insert task</button>
<h3>Here are the tasks:</h3>
<h4>Unchecked:</h4>
{this.props.root.map((item,index)=>{
if(item.isChecked===0)
return (<div><span key={index}>{item.data}</span><input type='checkbox' onClick={()=>this.props.changeCheck(item.InsertionId)}></input></div>)
return '';
})}
<h4>Checked:</h4>
{this.props.root.map((item,index)=>{
if(item.isChecked===1)
return (<div><span key={index}>{item.data}</span><input type='checkbox' checked="checked" onClick={()=>this.props.changeCheck(item.InsertionId)}></input></div>)
return '';
})}
</div>
)
}
}
const matchStatetoProps = (state) =>{
return {root:state.root};
}
const dispatchStatetoProps = (dispatch) =>{
return{
fetchtsk:()=> dispatch(fetchTasks),
changeCheck: (InsertionId)=> dispatch({type:'CHANGECHK',payload:InsertionId})
}
}
export default connect(matchStatetoProps,dispatchStatetoProps)(Homepage);
Now this will display an output like this:
Now when I check the checkbox, it should update the state of the corresponding row and put it in checked columns, basically updating the isChecked state. The reducer is here:
var initState=[];
const rootReducer = (state=initState,{type,payload})=>{
switch(type){
case 'DISPLAY':
var ar=[];
payload.map((item)=>{
ar.push({insertionId:item.InsertionId,data:item.Data,isChecked:0});
return ar;
});
return ar;
case 'CHANGECHK':
??????????????????????????????????
default:return state;
}
}
export default rootReducer;
could someone help me out?