pmndrs / valtio

💊 Valtio makes proxy-state simple for React and Vanilla
http://valtio.pmnd.rs
MIT License
8.7k stars 244 forks source link

About Rerendering ,Is it a bug or my usage error,Request help #717

Closed ljianc closed 1 year ago

ljianc commented 1 year ago
  1. Index component only access snapshot.loading
  2. Content component only set state .message
  3. Why Index component Rerendering 3 times, My understanding should be 2 times
const state = proxy({ loading: true, message: "" });

function Index() {
  const snapshot = useSnapshot(state);
  const Node = snapshot.loading ? Loading : Content;
  useEffect(() => {
    console.log("Log Index Rerendering");
  });
  useEffect(() => {
    state.loading = false;
  }, []);
  return <Node />;
}

function Loading() {
  return <>loading</>;
}

function Content() {
  useEffect(() => {
    //If this is added,Index Rerendering 3 times
    state.message = "welcome";

    //If this is removed,Index Rerendering 2 times
    //state.message = "welcome";
  }, []);
  return <>test</>;
}
dai-shi commented 1 year ago

Please continue discussion in #716.