skip to Main Content

I have URL like http://localhost:3000/aaa/bbb/ccc and each time i refresh the /bbb part is changed. How can I get /bbb segment and current value of /bbb as a string inside return() in React Native

const url = new URL("http://localhost:3000/aaa/bbb/ccc");

I try this and didnt work thank you

2

Answers


  1. Chosen as BEST ANSWER

    sorry for bad informations but i made it

      const [data, setData] = useState('');
             
    
      const fetchX = () => {
        axios.get('http://localhost:3000/aaa/bbb/ccc')
          .then((response) => {
           setData(response.data.ITSTHEURL)
            
          });
      };
     
      useEffect(() => {
        fetchX();
      }, []);
    
      return (
        <View style={{ paddingTop: 60 }}>
                
    
                <Text>{data.split("/")[2]}</Text>


  2. You can do it by separating the URI

    const url = new URL("http://localhost:3000/aaa/bbb/ccc");
    
    const splitUrl = url.pathname.split("/") // Output: ["", "aaa", "bbb", "ccc"]
    
    const selectB = splitUrl[2] // Output: "bbb"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search