2

I am new to react js. Here, I have following components where it is not getting called.

    export const BG: React.FunctionComponent<Icard> = ({
      const state = useLocalStore(() => ({
        id = url
        setId(url) {
          state.id = url
        }
      }))

      const changeImage = () =? {

      }

      React.useEffect(() => {
        state.setId(id)
      }, [id])

      const bgWrapper = (
        <div
          className={classNames({
            [css.ImageContainer]: true,
            [css.defaultImage]: !state.imageURI
          })}>
          {isUploadMode ? (
            <upload
              imageUrl={state.imageURI}
              changeImage={changeImage}
          ) : (
            <BImage
              image={defaulImage}
            />
          )}
        </div>
      )

      return useObserver(() => (
        <div className={css.BgCard}>
          {isUploadMode ? (
            bgWrapper
          ) : (
            <p>Hello</p>
          )}
        </div>
      ))
    }) 

Now here useEffect is not getting called . Now if I use this without creating a jsx vaiable. and use that div directly then it works. Can any one help me why the useEffect is not getting called .

ganesh kaspate
  • 1
  • 9
  • 41
  • 88
  • 1
    where's `id` defined? – Ramesh Reddy Apr 17 '20 at 06:04
  • Effects are guaranteed to be called at least once when the component mounts. Are you saying it isn't being called at all, ever? or it isn't triggering as expected? Can you clean up your code formatting? It's extremely unreadable. – Drew Reese Apr 17 '20 at 06:14
  • What do you expect to happen? What is `id`? You aren't defining it anywhere. – JMadelaine Apr 17 '20 at 06:47
  • It is getting called but not on change of id – ganesh kaspate Apr 17 '20 at 06:56
  • He is using Mobx's hook, useLocalStore which creates `id` in `state`. I think OP should not touch Mobx until he is more comfortable with the basic react hooks. See [suggestion](https://reactjs.org/docs/faq-state.html#should-i-use-a-state-management-library-like-redux-or-mobx). – Ben Butterworth Apr 17 '20 at 07:07
  • It doesn't appear as though `id` is in scope, but I'm surprised you are not seeing errors about it, so perhaps it is. Have you tried `state.id` *as* the dependency? What is updating `id` (or `state`) to *possibly* cause a rerender? – Drew Reese Apr 17 '20 at 15:46

3 Answers3

1

You need to remove id from the dependencies array at the end of useEffect. The purpose of that array is to list all the variable that when changed would trigger the effect that you define.

If you have the id there -- you are in a catch 22 and can't change it unless it's changed.

    React.useEffect(() => {
            state.setId(id)
          }, [])
rtviii
  • 807
  • 8
  • 19
0

Because you have a bug in your code?

const changeImage = () =? {

  }
Lekoaf
  • 897
  • 7
  • 11
0

What is the point of setting the ID state with the ID state? You should state.setId() in response to an action (button being pressed), not a state change.

My previous answer: The 2nd argument [id] to useEffect is an array of state values that cause it to run the callback (first argument).

  React.useEffect(() => {
    state.setId(id)
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [id])

Note: It would have been automatically added by ESLint, so what I do is add // eslint-disable-next-line react-hooks/exhaustive-deps as shown above.

According to this SO question/ answer, you might be using it in an unidiomatic way.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167