how to change params value passed to axios route to change data react native?

邮差的信 提交于 2020-05-17 05:56:26

问题


I am building a kind of book app (Holy Quran )... user will go for a list of Surahs, each Surah contains around 5 - 50 pages. I managed navigating user from the list to first page of each Surahs.. and through getting an api request data for first page will be shown and this is the code in the showScreen

    const [quran, setQuran] = useState([]);

    const page = navigation.getParam('page');
    const name = navigation.getParam('name');

    let pageNumber = page;

   useEffect(() => {
     Quran(); 
   }, []);


      const Quran = async () => {
        const response = await QuranApi.get(`/${pageNumber}/quran-uthmani`);
        setQuran(response.data.data.ayahs);
   }

so let's imagine that first page is page number 200, I am looking for some way so when user clicks go to page 201 or 199 (next or previous) and refetching the data so show for him requested page

I need some help here please and thanks in advance


回答1:


Basically you need to add some sort of button or any element in your 'markup section' which will trigger next/previous action. For example:

// The following line makes react listen for changes in page state variable.
// If you use setPage() anywhere, react will auto update all the components
// where page variable is used. You don't need to manually do it
const [page, setPage] = useState(nav.getParam('page'))

// ....

// This function does exactly that
const handleClick = event => { 
    if(event.target.value === 'next-page')  // If next button was pressed
        setPage(page + 1) // We increment the page state variable by 1
    else if(event.target.value === 'prev-page') // If prev button was pressed
        setPage(page - 1) // We decrement the page state variable by 1
    // By doing so, react will auto update every element which uses this variable
}

// .....

// We tell react, if [page] state variable changes by user clicking
// next or previous button, fetch data from the api using updated
// page number and using that, we update the [quran] variable using
// 'setQuran()' function
useEffect(async () => {
    const response = await QuranApi.get(`/${page}/quran-uthmani`)
    setQuran(response.data.data.ayahs)
}, [page] ); 

//......

// markup section
return(
    //....
    <Button onClick={handleClick} value="next-page">Next Page {page + 1}</Button>
    <Button onClick={handleClick} value="prev-page">Prev Page {page - 1}</Button>
    //....
)



回答2:


Thank you dear @Sayed it finally works but I have to make some modifications with the same idea to be

const [page, setPage] = useState(navigation.getParam('page'))
  const handleRightClick = () => {
        setPage(parseInt(page) + 1)
    };

    const handleLeftClick = () => {
        setPage(parseInt(page) - 1);
    }
   useEffect(() => {
        return () => {
          console.log("cleaned up");
        };
      }, []);

    useEffect(() => {
        Quran();
   }, [page]);

  <Button title = 'Next Page' onPress = {handleRightClick}/>
        <Button  title = 'Prev-page' onPress ={handleLeftClick} />

So it's working well in that case without an error




回答3:


Try this small change...


const [page,setPage]=useState(nav.getParam('page'))

useEffect(() => { 

    Quran(); 

}, [page]); 

by passing an empty array as a second parameter to the useEffect function, you are telling react to only execute the side effect once (at mount time).

However if that array contains a list of state variables to watch for. React will only re-run the side effect if one of the items in this array changes.

The above mentioned change in useEffect would make your useEffect sensitive to any change in the state variable (page) passed to the useEffect array. As you pass new pagenumbers in the url as a parameter, it would result in calling useEffect everytime as the page state variable is set.

Hope this helps!



来源:https://stackoverflow.com/questions/61645516/how-to-change-params-value-passed-to-axios-route-to-change-data-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!