Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【React】custom-element set Object/Array prop in React Hook, the operation result is unstable #253

Open
my9074 opened this issue Apr 25, 2021 · 0 comments

Comments

@my9074
Copy link

my9074 commented Apr 25, 2021

@karol-f

When prop needs to be of object/array type, it needs to be operated through JS. However, the rendering of custom-element in React Hook does not seem to be consistent with the execution logic of useEffect after component rendering. The phenomenon is that the effect function is executed earlier than the custom-element, causing the setting prop to not work correctly

CodeSendbox demo

It cannot be reproduced in CodeSendbox, but it is not normal locally. React version is consistent

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  const [stringProp, setStringProp] = useState("stringProp");
  const [booleanProp, setBooleanProp] = useState(true);
  const [numberProp, setNumberProp] = useState(1);

  useEffect(() => {
    // this is work in my local
    // setTimeout(() => {
    //   document.getElementsByTagName("table-wc")[0].items = [
    //     { prop: "1", value: "1", type: "1" },
    //     { prop: "2", value: "2", type: "2" }
    //   ];
    // }, 1000);

    // Not work in my local. I guess effect function was executed before table-wc created,
    // which caused the items to not be set up
    console.log(1111111);
    document.getElementsByTagName("table-wc")[0].items = [
      { prop: "1", value: "1", type: "1" },
      { prop: "2", value: "2", type: "2" }
    ];
  });

  return (
    <div className="App">
      <table-wc
        string-prop={stringProp}
        boolean-prop={booleanProp}
        number-prop={numberProp}
      ></table-wc>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Local:
image

Expected
image

@my9074 my9074 changed the title 【React】vue-custom-element set Object/Array prop in React Hook, the operation result is unstable 【React】custom-element set Object/Array prop in React Hook, the operation result is unstable Apr 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant