0

I would like to generate an array from my existing redux state with exported function in functional component. I want to use my useSelector data and dispatch function in my exported function. I only need the generate function that I can call from outside, but for using redux state, I have to create a functional component too.

To show you my problem, here is my code:

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { treeActions } from "../store/treedata-slice";

function GenerateTree() {
  const dispatch = useDispatch();
  const pathways = useSelector((state) => state.tree.pathways);
  const pickedRenderElementType = useSelector(
    (state) => state.tree.pickedRenderElementType
  );
  const pickedRenderElementValue = useSelector(
    (state) => state.tree.pickedRenderElementValue
  );
  const scenarios = useSelector((state) => state.tree.scenarios);
  const treeData = useSelector((state) => state.tree.treeData);
  
  return <div></div>;
}

export const generate = () => {
  const arr = scenarios;
  if (pickedRenderElementType === "scen") {
    dispatch(treeActions.setTreeData(/* make the tree */));
  }
  if (pickedRenderElementType === "path") {
    dispatch(treeActions.setTreeData(/* make the tree */));
  }
  if (pickedRenderElementType === "act") {
    dispatch(treeActions.setTreeData(/* make the tree */));
  }
};
export default GenerateTree;

enter image description here

1 Answer 1

1

useSelector and dispatch cannot access outside component function. Instead, we can pass dispatch and redux state as arguments.

Add dispatch to the props of the function and then add it as well when ever you call the function.

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { treeActions } from "../store/treedata-slice";

function GenerateTree() {
  const dispatch = useDispatch();
  const pathways = useSelector((state) => state.tree.pathways);

  const pickedRenderElementType = useSelector(
    (state) => state.tree.pickedRenderElementType
  );

  const pickedRenderElementValue = useSelector(
    (state) => state.tree.pickedRenderElementValue
  );

  const scenarios = useSelector((state) => state.tree.scenarios);
  const treeData = useSelector((state) => state.tree.treeData);
  
  return <div></div>;
}

export const generate = (dispatch) => {
  const arr = scenarios;

  if (pickedRenderElementType === "scen") {
    dispatch(treeActions.setTreeData(/* make the tree */));
  }

  if (pickedRenderElementType === "path") {
    dispatch(treeActions.setTreeData(/* make the tree */));
  }

  if (pickedRenderElementType === "act") {
    dispatch(treeActions.setTreeData(/* make the tree */));
  }
};

export default GenerateTree;
Sign up to request clarification or add additional context in comments.

3 Comments

Can you show me how can you pass dispatch and redux state as an arguments? I tried but it gives me undefined value in the generate function.
function GenerateTree() { const dispatch = useDispatch(); const state = useSelector((state) => state); const pickedRenderElementType = useSelector( (state) => state.tree.pickedRenderElementType ); function handler(){ generate(dispatch,state) } return <div></div>; } export const generate = (dispatch,state) => { /*processs*/ }; you can call function from only other component.
Not working, return undefined values when i console log dispatch or state. I call it from an other component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.