v2.5.2
Giriş yap

Redux ile başka bir slicedan aldığım veri ile başka slice üzerindeki api linkini nasıl değiştirebilirim

mlhslckr
226 defa görüntülendi

React redux ile başka bir slicedan aldığım veri ile başka slice üzerindeki api linkini nasıl değiştirebilirim ?

KoySlice.js


const initialState = {
  koy: [],
};

export const getKoy = createAsyncThunk("getKoy", async () => {
  const { data } = await axios.get(
    "https://localhost:44381/api/Koys/getbyilce?ilceid="
  );
  return data;
});

export const koySlice = createSlice({
  name: "koy",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(getKoy.fulfilled, (state, action) => {
      state.koy = action.payload;
    });
  },
});

export default koySlice.reducer;

IlceSlice.js



export const getIlce = createAsyncThunk("getIlce", async () => {
  const res = await axios("https://localhost:44381/api/Ilces/getall");
  const data = await res.data;
  return data;
});

export const ilceSlice = createSlice({
  name: "ilce",
  initialState: {
    ilce: [],
    isLoading: false,
    error: null,
    value: 0,
  },
  reducers: {
    selectValue: (state, action) => {
      state.value = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(getIlce.pending, (state) => {
      state.isLoading = true;
    });
    builder.addCase(getIlce.fulfilled, (state, action) => {
      state.isLoading = false;
      state.ilce = action.payload;
    });
    builder.addCase(getIlce.rejected, (state, action) => {
      state.isLoading = false;
      state.error = action.error.message;
    });
  },
});

export default ilceSlice.reducer;

export const { selectValue } = ilceSlice.actions;


Ilce.js

const Ilce = () => {
  const { ilce } = useSelector((state) => state.ilce);
  const isLoading = useSelector((state) => state.isLoading);
  const error = useSelector((state) => state.error);
  const count = useSelector((state) => state.ilce.value);
  const dispatch = useDispatch();

  useEffect(() => {
    dispatch(getIlce());
  }, []);

  const ilces = ilce.data;

  return (
    <div className="input">
      <h2 className="title">İlçeler</h2>
      <div>
        {
          <select
            className="select"
            onChange={(e) => console.log(dispatch(selectValue(e.target.value)))}
          >
            <option value="">--Seçiniz--</option>
            {ilces?.map((ilce) => (
              <option key={ilce.id} value={ilce.id}>
                {ilce.adi}
              </option>
            ))}
          </select>
        }
      </div>
    </div>
  );
};

export default Ilce;

Ilce.js den aldığım selectValue değerini nasıl KoySlice.js da api linki sonuna eklerim ?

Cevap yaz
Cevaplar (0)
Henüz kimse cevap yazmadı. İlk cevap yazan sen ol!