Weird behavior with react-compiler eslint #1099
Replies: 2 comments 3 replies
-
|
I'm not sure this is a valtio bug...Just a guess here, but are you defining the proxy state inside of a component? I'm not sure if you're using typescript or not...and this might not solve your issue...but when i have state that can be undefined at different times, I like to keep track of that kind of thing as well...including stuff like loading state...kinda like this: import { proxy } from 'valtio'
type DataState<T> =
| { status: 'loading' }
| { status: 'error', error: Error }
| { status: 'success', data: T }
interface AudioPlayer {
audioId: number
}
const initialState: DataState<AudioPlayer> = {
status: 'loading',
};
const audioPlayerState = proxy<DataState<AudioPlayer>>(initialState)
//...
audioPlayerState.status = 'success'
audioPlayerState.data = {
audioId: 1
} |
Beta Was this translation helpful? Give feedback.
-
|
This is most likely a react-compiler bug. React Compiler still has a few weird quirks. It's going to rewrite whatever code you write so it's bound to run into issues like this sometimes. For example: import { useEffect } from "react";
const C = () => {
useEffect(() => { a(); }, []);
function a () { window.x = "" }
return <div></div>;
}
export default C
// throws the same error
// InvalidReact: Writing to a variable defined outside a component or hook is not allowed. Consider using an effect (4:4)but if you just move the function declaration above the useEffect call, all is fine... import { useEffect } from "react";
const C = () => {
function a () { window.x = "" }
useEffect(() => { a(); }, []);
return <div></div>;
}
export default C
// no error!functoins in js are hoisted, however, you're not actually writing your own code. Your code is compiled into something thaqt bareely ever recognizedn you're so this just an issue with react-compiler. I imagine something similar is happening with you. Unfortunatly, you're at its whim if you want to use it. Here's an example of what I'm talking about: https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBUYCAogGaUJyEC+BlMEGBAOiDAgIZ2cBudgDs4EYWEIBhAgF4CACgCUcgHxERBEmSo06C5WqIEeygQXoAaAgG0AukqHCtlKKLxpxJxSuIB3NGEAEwg-ADoADzkOEE4LTQJuPFhnAB4gtAA3VVSAegzsp3oRBAicfAIghEoeKAAbaRB6IA You might just need to rearrange your code a bit until the compiler is happy. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug Description
// this is my simplified version of my code
export const audioPlayerState = proxy({
audioId: undefined,
});
const play = () => {
audioPlayerState.audioId = audio.id;
};
const stop = () => {
audioPlayerState.audioId = undefined; // error occurs only here
};
the lint error says
Writing to a variable defined outside a component or hook is not allowed. Consider using an effect react-compiler/react-compilerthis is weird becuz if this error is right, the error should also occur on
playfunction and it doesn't. have you got any clues for it? the only solution i've got was to eslint ignoreReproduction Link
sorry for reporduction linkt
Beta Was this translation helpful? Give feedback.
All reactions