search

Home  >  Q&A  >  body text

Pass data from component to Util file

I'm trying to pass data from one React component to another and trying to understand how to call this data to the target file

Basically you want to pass the <input /> value from component.js to a function in the Util.js file.

So far I have tried the following in component.js:

const [distance, setDistance] = useState();

function handleChange(value){
    setDistance(value.target.value);
}

return {
    <input className="input" name="distance" type={`number`} onChange={handleChange} />
}

Util.js

export const filterByDistance = function(){       
}

So to call it do I need to do something like this?

import {distance} from './component.js';

P粉163465905P粉163465905265 days ago435

reply all(1)I'll reply

  • P粉274161593

    P粉2741615932024-02-22 16:29:54

    Don't try to import variables from component.js into Utils.js, you should do it the other way around: into the Util.js function Import component.js and call it.

    import { filterByDistance } from './Util.js';
    
    const [distance, setDistance] = useState();
    
    function handleChange(value){
        setDistance(value.target.value);
        filterByDistance(value.target.value)
    }
    
    return { ... }

    Then accept the value as parameter:

    export const filterByDistance = function(value){
        console.log(value)
    }

    reply
    0
  • Cancelreply