How to access the state (from the store) in a Slice (Redux Toolkit) in react

I’m experimenting with an App with Redux-Toolkit only and one with a JSON Server in combination with AXIOS. They are both in the same App.

This is why there are some reducers which look very similar in naming, if you are wondering about the strange names.

The _axios_j ones are what is this topic about.

In the reducer upTask I would like to take and access the actual array with my Tasks from the state “tasklist”.

The state has been filled with a JSON file with axios GET request in the App.js.

My issue is, that in my taskSliceAxios I don’t have that state with the actual full state and somehow I cannot use useSelector() because this component isn’t a function.

The payload I get from the button on my form just contains the specific task and its ID.

store.js

import { configureStore } from "@reduxjs/toolkit";
import taskSliceReducerAxiosJ from '../features/taskSliceAxiosJ';
import loginSliceReducerAxiosJ from "../features/loginSliceAxiosJ";
import taskSliceReducerReduxTk from '../features/taskSliceReduxTk';
import loginSliceReducerReduxTk from '../features/loginSliceReduxTk';

const store = configureStore({
  reducer: {
    task_axios_j: taskSliceReducerAxiosJ,
    login_axios_j: loginSliceReducerAxiosJ,
    taskreduxtk: taskSliceReducerReduxTk,
    loginreduxtk: loginSliceReducerReduxTk
  }
})

export default store

Here I would like to take the actual array with the tasks from the state and exchange this items/index which requires this state if I’m right. Could it be my thoughts are going the totally wrong direction?

This here is my View, where i map the array
taskSliceAxios.jsx

import { createSlice } from "@reduxjs/toolkit";
import axios from 'axios'
import { BASE_URL } from '../../assets/database/baseURL';

// Initial State for task
const initialState = {
  task: []
}

const taskSliceAxiosJ = createSlice({
  name: 'task_axios_j',
  initialState,
  reducers: {
    upTask: (state, action) => {
      const index = action.payload
      const moveUp = (ele, index) => {
        mover()

        if (index === 0) return; 
        [state.task[index - 1], state.task[index]] = [state.task[index], state.task[index - 1]];
      };

      axios.patch(`${BASE_URL}/tasks/${index}`, { moveUp })
        .then((response) => {
          console.log(response)
        })
    },

My View Component “TaskAxiosJ.jsx
Here the Button “UP” that calls the dispatchEvent():
(i posted the complete componente here now)

<tbody>
  {props.tasklist.map((ele, id) => {
    return (
      <>
        <tr>
          <td key={ele.id} className="Tasks">
            {ele.Task}
            <Button
              variant="outline-danger"
              className="button"
              onClick={() => dispatchEvent(removeTask(ele.id))}
            >
              DELETE
            </Button>                                     
            <Button
              variant="outline-secondary"
              className="button"
              onClick={() => dispatchEvent(upTask(ele))}
            >
              UP
            </Button> 
          </td>
        </tr>
      </>
    )
  })}
</tbody>

Here the App.jsx where call “useEffect” to load my JSON File for one single time and call the Setter for my tasklist

import TaskAxiosJ from './components/TaskAxiosJ'
import TaskReduxTk from './components/TaskReduxTk'
import 'bootstrap/dist/css/bootstrap.min.css'
import { Routes, Route } from 'react-router-dom'
import Home from './components/Home'
import Contact from './components/Contact'
import { useEffect, useState } from 'react'
import axios from 'axios'
import { BASE_URL } from './assets/database/baseURL'



function App() {


  const [taskValue, setTaskValue] = useState("");
  const [tasklist, setTasklist] = useState([]);




  useEffect(() => {
    axios.get(`${BASE_URL}/tasks`)
      .then((response) => {
        setTasklist(response.data)
        //setLoadingData(true)
      })

      .catch((error) => {
        alert(error.message)
    
      //  setLoadingData(false)
      })
  }, [])



  return (
    <>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/Contact" element={<Contact />} />
        <Route path="/TaskAxiosJ" element={<TaskAxiosJ
          setTasklist={setTasklist} setTaskValue={setTaskValue}
          tasklist={tasklist} taskValue={taskValue}
         // loadingData={loadingData}

        />} />
        <Route path="/TaskReduxTk" element={<TaskReduxTk />} />

      </Routes>

    </>
  )
}

export default App

I already tried to wrap my complete const taskSlicheAxiosJ in a enclosing function, so I can use useSelector(). I also tried different spots in the slice where to place the constant which gets the actual state.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật