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

Infinite Loop in functional component #71

Open
invious opened this issue Nov 18, 2019 · 1 comment
Open

Infinite Loop in functional component #71

invious opened this issue Nov 18, 2019 · 1 comment

Comments

@invious
Copy link

invious commented Nov 18, 2019

const ArticleForm = (props) => {

    const {article} = props
    const classes = useStyles();
    const dispatch = useDispatch()
    const router = useRouter()


    const [editorState, setEditorState] = useState(EditorState.createEmpty());


    const blocksFromHtml = htmlToDraft(article.body);
    const { contentBlocks, entityMap } = blocksFromHtml;
    const contentState = ContentState.createFromBlockArray(contentBlocks, entityMap);
    const loadedEditorState= EditorState.createWithContent(contentState);

    debugger

    setEditorState(loadedEditorState)

the setEditorState is causing this error:

image

@ilyador
Copy link

ilyador commented Nov 18, 2019

useState sets an initial state, and setEditorState changes it right after.
State change causes the component to re-render, and once it is rendered again, the state is changed again putting it in an infinite re-render loop.

You need to use useEffect to make sure your state change runs before the component is mounted. preventing another render.

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

2 participants