简体   繁体   中英

How i can use react hooks with native SSR (without NextJS)?

I make custom SSR (nodeJS, ReactJS, Webpack) and i have trouble with React hooks.
When i try use hooks for example useState (her secound arguments), i'll get error "setGreetingsBlockData is not a function" on server console.

// Code from component
const { greetingsBlockData, setGreetingsBlockData } = useState({})

useEffect(() => {
    fetch(
        `https://adventuretimeapi.herokuapp.com/people`,
        { method: "GET" }
    )
        .then(res => res.json())
        .then(data => setGreetingsBlockData(data))
}, [])

setGreetingsBlockData({test: 1})

How i can fix it?

useState returns an array, not an object

const [ greetingsBlockData, setGreetingsBlockData ] = useState({})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM