score:0
Just check reducer
, u did not anything with changeStatus
!
score:0
Try change your ListTaskComponent
to below should solve the issue, not need useState
just use them directly.
const ListTaskComponent = props => (
<section id="wrap-task" className="mt-3">
{props.taskState.tasks.map(value =>
<TaskItemComponent key={value.id} task={value}/>
)}
</section>
)
score:1
The problem is most likely that you're violate the "single source of truth" principal.
You do this with the following lines of code:
const ListTaskComponent = props => { const [tasks, setTasks] = useState([]); useEffect(() => { setTasks(props.taskState.tasks); }, []);
So, what is the plan here? You receive a list of taskState.tasks
from the Redux store. Which is is copied into the components own state. What happens if taskState.tasks
changes? The ListTaskComponent
component will re-render, but since tasks
is only set on component mount the value will never update.
The problem is easily fixed by removing the local state.
const ListTaskComponent = props => {
const tasks = props.taskState.tasks;
The above should fix the "single source of truth" principal. However if the first thing we do is map the state to a value you might as well move this change into mapStateToProps
to simplify the actual component.
const ListTaskComponent = ({ tasks }) => {
const list = tasks.map(value =>
<TaskItemComponent key={value.id.toString()} task={value} />
);
return (
<section id="wrap-task" className="mt-3">{list}</section>
)
}
const mapStateToProps = state => ({ tasks: state.taskReducer.tasks });
export default connect(mapStateToProps, null)(ListTaskComponent);
Another issue I can see has to do with the reducer itself. Reducers should not mutate the previous state, yet in your code I can spot mutations to the original state:
let list = state.tasks; list.push(action.payload);
let list = state.tasks; let index = list.findIndex((value => value.id === action.payload)); if (index > -1) { list[index].status = true;
let list = state.tasks; let index = list.findIndex((value => value.id === action.payload)); if (index > -1) { list.splice(index, 1);
I suggest reading Writing Reducers which explains the rules that reducers should follow and also gives some examples.
Coming back to your code you could write the above code blocks in the following manner:
let list = [...state.tasks];
list.push(action.payload);
// or
let list = [...state.tasks, action.payload];
let list = [...state.tasks];
let index = list.findIndex((value => value.id === action.payload));
if (index > -1) {
list[index] = { ...list[index], status: true };
// or
let list = state.tasks.map(value => {
if (value.id !== action.payload) return value;
return { ...value, status: true };
});
let list = [...state.tasks];
let index = list.findIndex((value => value.id === action.payload));
if (index > -1) {
list.splice(index, 1);
// or
let list = state.tasks.filter(value => value.id !== action.payload);
Source: stackoverflow.com
Related Query
- Component not re-render when dispatch action in Redux
- How to dispatch Redux action from stateless component when route is loaded?
- redux action "is not a function" when dispatched from component
- React Redux - How to dispatch an action on componentDidMount when using mapDispatchToProps in a connected component
- Dispatch is not a function when testing a redux action
- React component not refreshing when redux state has been updated with successful action
- How do i dispatch a redux action from class and not component
- Prevent component from re-rendering when property not used in render function React Redux
- Redux - Reducer is not being called when I dispatch an action
- Component not updating when redux store modified
- React redux dispatch action before render container
- Redux action return type when using dispatch and getState
- How to make MobX update the component when observable is not used in the render method?
- Redux action dispatch not working
- Best practice when not needed to render the component
- React hooks: Parent component not updating when dispatch is called in a child component
- Redux - reducers / action creators not linked properly to component
- Ref.current is undefined when component mount and it can not cause a re render when I need it
- Why Redux returns old state when dispatch action before?
- React component not updating on final dispatch and update of redux state
- How to pass Dispatch to action when using redux form
- dispatch action triggering, but redux store not updating
- useReducer: dispatch action, show state in other component and update state when action is dispatched
- Redux thunk not resolving before next action + then is not a function or dispatch undefined
- Dispatch async redux action from non-react component with thunk middleware
- React - render component when clicking on a component which is not its child
- What pattern to use when a component only needs to dispatch an action creator?
- How to dispatch an action when another synchronous action is complete in Redux
- Redux Thunk + ReactJS: Action creator being called but dispatch function not being called
- React Redux action not entering into dispatch
More Query from same tag
- having trouble deploying react app with mongo db to heroku
- React protected route
- Save search term on refresh React
- How to manually navigate to route with React + React-Router-Component
- Configure JSX User Snippets in VS Code
- How to pass arguments to callback in React + Typescript without new function creation?
- React - displaying images side by side
- How to add a clip path to image in tailwind
- Collapse each button uniquely, instead of all at once
- How to change image every second in map react
- React Router + iis? How to enable routing
- TypeError: Cannot read property 'component' based on id
- How to use throttle function after value change, after couple of seconds?
- Import react-table into ClojureScript with Shadow-CLJS
- React beautiful drag and drop doesn't work with Material UI component
- Update a redux array of object but not re-render the component
- How to make a button in react.js to make a text toggle from appearing to disappearing?
- I can't change route from Primereact menu
- windows Authorization in ReactJS and .NET Core API
- Loading Image in a React+Django app
- React render string as HTML in ant design
- Get value of another column in an ant.design Table's column?
- How to create a single Gatsby Page to show and filter all blog posts by tag/category
- How to create email templates in React JS using Rails as backend
- Passing props from child to parent(without context)
- using child text of an element to set/update a variable
- How to enable tooltip on React-vis?
- How can I nest this Obejct inside another using React/Formik and Yup validation?
- React.js Problem to sent props and event from parent to child
- I don't understand how to append JSON object data to an Accordion