Skip to content
Advertisement

How to bind states and functions in different components without Redux?

I have a Main page, it has the findUser() and inputValue values, I want to make a static application header, but it has an input that requires findUser() and inputValue to use it, how can I pass them correctly (this can be done without Redux ?)

Main page:

const githubPageref = useRef(1);
  const reposRef = useRef([]);
  const [userNickName, setUserNickName] = useState('');
  const [userProfile, setUserProfile] = useState('');
  const [repos, setRepos] = useState([]);
  const [inputValue, setinputValue] = useState('');
  const [reposCount, setReposCount] = useState(0);
  const [page, setPage] = useState(1);
  const [currentRepos, setCurrentRepos] = useState([]);
  const pageSize = 4;
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();

  const findUser = async () => {
    setLoading(true);
    setUserNickName(inputValue);
    const reposUrl = `${usersUrl + inputValue}/repos?page=${githubPageref.current}`;
    const profileUrl = usersUrl + inputValue;
    await getApiResource(reposUrl)
      .then((data) => {
        if (!data) { navigate('UserNotFoundPage'); }
        setRepos([...data, ...repos]);
        reposRef.current = ([...repos, ...data]);
      });
    await getApiResource(profileUrl)
      .then((data) => {
        setUserProfile(data);
        setLoading(false);
      });
  };

App:

const App = ({ findUser, setinputValue }) => {
  return (
    <>
      <Header
        findUser={findUser}
        setinputValue={setinputValue}
      />
      <Routes>
        <Route path="/" element={<MainPage />} />
        <Route path="a" element={<StartSearchingPage />} />
        <Route path="UserNotFoundPage" element={<UserNotFoundPage />} />
      </Routes>
    </>
  );
};

Advertisement

Answer

You could use the Context API from React as an alternative to Redux. Or simply, you could define what is need in MainPage.js and Header.js in App.js and pass them down. As an example this way:

const App = ({ findUser, setinputValue }) => {
  const githubPageref = useRef(1);
  const reposRef = useRef([]);
  const [userNickName, setUserNickName] = useState('');
  const [userProfile, setUserProfile] = useState('');
  const [repos, setRepos] = useState([]);
  const [inputValue, setinputValue] = useState('');
  const [reposCount, setReposCount] = useState(0);
  const [page, setPage] = useState(1);
  const [currentRepos, setCurrentRepos] = useState([]);
  const pageSize = 4;
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();

  const findUser = async () => {
    setLoading(true);
    setUserNickName(inputValue);
    const reposUrl = `${usersUrl + inputValue}/repos?page=${githubPageref.current}`;
    const profileUrl = usersUrl + inputValue;
    await getApiResource(reposUrl)
      .then((data) => {
        if (!data) { navigate('UserNotFoundPage'); }
        setRepos([...data, ...repos]);
        reposRef.current = ([...repos, ...data]);
      });
    await getApiResource(profileUrl)
      .then((data) => {
        setUserProfile(data);
        setLoading(false);
      });
  };
  return (
    <>
      <Header
        findUser={findUser}
        setinputValue={setinputValue}
      />
      <Routes>
        <Route path="/" element={<MainPage />} />
        <Route path="a" element={<StartSearchingPage />} />
        <Route path="UserNotFoundPage" element={<UserNotFoundPage />} />
      </Routes>
    </>
  );
};
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement