Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/790b02c3a6535dcb7c64466f132a2141 to your computer and use it in GitHub Desktop.
Save kenmori/790b02c3a6535dcb7c64466f132a2141 to your computer and use it in GitHub Desktop.
解決【React】Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.」

解決【React】Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.」

from

const Contents = (props: ContentsProps) => {
    return (
        <div key={props.title}>
            {props.contents.map( // here!!!
                renderFunc({
                    title: props.title,
                    listId: props.listId
                })
            )}
        </div>
    )
}

const renderFunc = ({
    title,
    handleDelete,
    handleOnBluer,
    listId
}: {
    title: string
    listId: number
}) => (content: Entity.ContentType) => {
    const [state, setValueState] = React.useState<{
        [contentId: number]: string
        listId: number
    }>({
        [content.id]: content.value,
        listId
    })
    const onChangeNative = React.useCallback(
        content => (e): void => {
            setValueState({ ...state, [content.id]: e })
        },
        [setValueState, state]
    )
    return (
        <div key={`${content.id}`}>
            <ContentWrapper>
                <Input
                    format="text"
                    value={state[content.id]}
                    onChange={onChangeNative(content)}
                    onBlur={handleOnBluer(state, listId)}
                    name={name}
                    errored={content.errored}
                    errorMessage={content.errorMessage}
                />
            </ContentWrapper>
        </div>
    )
}

to

const Contents = (props: ContentsProps) => {
   return (
       <div key={props.title}>
           {props.contents.map(content => ( // change to below!!!!!!!!!!!!!!!!!!!!!
               <Renderfun
                   title={props.title}
                   content={content}
                   listId={props.listId}
               />
           ))}
       </div>
   )
}

const Renderfun = ({
   title,
   content,
   listId
}: {
   title: string
   content: Entity.ContentType
   listId: number
}) => {
   const [state, setValueState] = React.useState<{
       [contentId: number]: string
       listId: number
   }>({
       [content.id]: content.value,
       listId
   })
   const onChangeNative = React.useCallback(
       content => (e: any): void => {
           setValueState({ ...state, [content.id]: e })
       },
       [setValueState, state]
   )
   return (
       <div key={`${content.id}`}>
           <ContentWrapper>
               <Input
                   format="text"
                   value={state[content.id]}
                   placeholder={`${title}を入力`}
                   name={name}
                   errored={content.errored}
                   errorMessage={content.errorMessage}
               />
           </ContentWrapper>
       </div>
   )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment