Reactiflux
Reactiflux

help-threads-react

Root Question Message

EduardS
EduardS2/8/2023
How to solve calling hooks conditionally here?
I have this data coming from validateData that returns some data if valid otherwise null. I then check if data exists and return early otherwise. After that I use useCustomHook that depends on that data but because I returned early I get a warning that hook is called conditionally. How can I avoid this issue?

const MyComponent: FC<Props> = ({ rawData }) => {
  const data = validateData(rawData);
  if (!data) return null;

  const someOtherData = useCustomHook(data.something);

  return <div>{data.something}</div>;
};

export default MyComponent;
EduardS
EduardS2/8/2023
In return jsx should be someOtherData but yeah its just an example hope its clear enough
Message Not Public

Sign In and Join Server To See

2/8/2023
Message Not Public

Sign In and Join Server To See

2/8/2023
Message Not Public

Sign In and Join Server To See

2/8/2023
Message Not Public

Sign In and Join Server To See

2/8/2023
EduardS
EduardS2/8/2023
@1055077053369688195 I asked the question in react-forum as well and that answer helped me.
https://discord.com/channels/102860784329052160/1072888209383637073

Thanks for the other links they have been very informative!
EduardS
EduardS2/8/2023
@1063869053950758912 unfortunately I cant modify the useCustomHook so the best solution was to wrap it in another component like someone suggested in my other post https://discord.com/channels/102860784329052160/1072888209383637073
Message Not Public

Sign In and Join Server To See

2/9/2023
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy