Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
React State update doesn't get reflected inside a child component if interpolated into a string

I've got a problem where my component re-renders successfully but the state doesn't change inside the child components that receives the date interpolated inside a string

Example:

export const IntegrationPage = () => {

  const currentWorkspace = useSelector(getCurrentWorkspace);
  const workspaceState = useSelector(getWorkspaceState);

  const [workspaceId, setWorkspaceId] = useState(currentWorkspace?.identifier);

  useEffect(() => {
    setWorkspaceId(currentWorkspace?.identifier);
//if I console.log workspaceId here, it updates correctly as well.
  }, [currentWorkspace]);

  if (!currentWorkspace || workspaceState.status === Status.requesting || !workspaceId) {
    return <LoadingSpinner />;
  }
  return (
    <div className={styles.IntegrationPage}>
      <div className={styles.content}>
        <div className={styles.left}>
                                                           //This updates correctly
          <Card leftHeader="Tracking overview" rightHeader={`ID: ${workspaceId}`}>
            <CardItem content="No visits in last 48 hours" header="No data" variant="item" />
          </Card>
            <CodeBox>
             {`something here ${workspaceId} something else`}
            </CodeBox>
            ...
        </div> 
    </div>
  );
};

CodeBox:


type CodeBoxProps = {
  children?: ReactNode;
  src?: string;
};

export const CodeBox: FunctionComponent<CodeBoxProps> = ({ children, src }) => {
  const dispatch = useDispatch();
  const handleCopy = () => {
    const text = src?.toString() ?? children?.toString();
    navigator.clipboard.writeText(text ? text : "");
    dispatch(showNotification("Code was copied to clipboard", "success", "clipboard", 2250));
  };

  return (
    <div className={styles.codeBox} onClick={handleCopy}>
      <code className={styles.code}>{src ?? children}</code>
    </div>
  );
};

As you can see, I've tried passing the code inside a src prop instead of children but the error persists.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I "solved" it using redux and updating the action:

export const changeWorkspace = (workspaceId: number) => async (dispatch: any) => {
  await dispatch({
    type: WORKSPACE_CHANGE,
    workspaceId,
  });

  return dispatch({
    type: WORKSPACE_SET,
    workspaceId,
  });

  localStorage.setItem("selected-workspace", workspaceId);
};

//reducer:
   case WORKSPACE_CHANGE:
      return {
        ...state,
        status: Status.change,
        currentWorkspace: state.workspaces.find((workspace) => workspace.id === action.workspaceId),
      };

    case WORKSPACE_SET:
      return {
        ...state,
        status: Status.success,
      };

And then updating the loading spinner condition like so:

  if (!currentWorkspace || workspaceState.status === Status.change || !workspaceId) {
    return <LoadingSpinner />;
  }

Which forces to re-render the whole component each time I change the workspace. (Status becomes change so it returns the loading spinner for a split millisecond and then renders the component again with the correct values)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!