Unverified Commit 8881c413 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix useState function initialiser case for `optimize_server_react` transform (#55551)

This addresses the case mentioned in https://github.com/vercel/next.js/pull/54925#pullrequestreview-1616315332. We can't optimize the case where we can't statically determine the type of the `useState` argument as it can be a function: `useState(() => ...)`.
parent 10250119
This diff is collapsed.
......@@ -145,38 +145,50 @@ impl Fold for OptimizeServerReact {
for decl in d {
if let Pat::Array(array_pat) = &decl.name {
if array_pat.elems.len() == 2 {
if let Some(array_pat_1) = &array_pat.elems[0] {
if let Some(array_pat_2) = &array_pat.elems[1] {
if let Some(box Expr::Call(call)) = &decl.init {
if let Callee::Expr(box Expr::Ident(f)) = &call.callee {
if let Some(use_state_ident) = &self.use_state_ident {
if &f.to_id() == use_state_ident && call.args.len() == 1 {
// const state = x, setState = () => {};
new_d.push(VarDeclarator {
definite: false,
name: array_pat_1.clone(),
init: Some(call.args[0].expr.clone()),
span: DUMMY_SP,
});
new_d.push(VarDeclarator {
definite: false,
name: array_pat_2.clone(),
init: Some(Box::new(Expr::Arrow(ArrowExpr {
body: Box::new(BlockStmtOrExpr::Expr(
Box::new(Expr::Lit(Lit::Null(Null {
span: DUMMY_SP,
}))),
)),
params: vec![],
is_async: false,
is_generator: false,
if let (Some(array_pat_1), Some(array_pat_2)) =
(&array_pat.elems[0], &array_pat.elems[1])
{
if let Some(box Expr::Call(call)) = &decl.init {
if let Callee::Expr(box Expr::Ident(f)) = &call.callee {
if let Some(use_state_ident) = &self.use_state_ident {
if &f.to_id() == use_state_ident && call.args.len() == 1 {
// We do the optimization only if the arg is a literal or a
// type that we can
// be sure is not a function (e.g. {} or [] lit).
// This is because useState allows a function as the
// initialiser.
match &call.args[0].expr {
box Expr::Lit(_)
| box Expr::Object(_)
| box Expr::Array(_) => {
// const state = x, setState = () => {};
new_d.push(VarDeclarator {
definite: false,
name: array_pat_1.clone(),
init: Some(call.args[0].expr.clone()),
span: DUMMY_SP,
type_params: None,
return_type: None,
}))),
span: DUMMY_SP,
});
continue;
});
new_d.push(VarDeclarator {
definite: false,
name: array_pat_2.clone(),
init: Some(Box::new(Expr::Arrow(ArrowExpr {
body: Box::new(BlockStmtOrExpr::Expr(
Box::new(Expr::Lit(Lit::Null(Null {
span: DUMMY_SP,
}))),
)),
params: vec![],
is_async: false,
is_generator: false,
span: DUMMY_SP,
type_params: None,
return_type: None,
}))),
span: DUMMY_SP,
});
continue;
}
_ => {}
}
}
}
......
import { useState } from 'react';
export default function App({ x }) {
const state = 0, setState = ()=>null;
const state2 = ()=>0, setState2 = ()=>null;
const state3 = x, setState3 = ()=>null;
const [state2, setState2] = useState(()=>0);
const [state3, setState3] = useState(x);
const s = useState(0);
const [state4] = useState(0);
const { a } = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment