I have a function component (A) import another function component (B).
B has a onChange value, I want to get the onChange value all the time when I import B in A.
I have no idea how to get it.
Here is the code like this:
A:
import B from './B';
const A = () => {
// I want to get B's onChange value over here
return (
<B />
);
}
B:
import React, { useState } from 'react';
const B = () => {
const [value, setValue] = useState('');
return (
<SomePicker value={value} onChange={(value) => setValue(value)}
);
}
You can refer below solution.
import B from './B';
const A = () => {
const getOnChangeValueFromB = (data) => {
console.log(data)// you will get onChange value here
};
// I want to get B's onChange value over here
return (
<B callBack={getOnChangeValueFromB}/>
);
}
import React, { useState } from 'react';
const B = (props) => {
const [value, setValue] = useState('');
const returnValueToA = (value) => {
setValue(value)
props.callBack(value);
}
return (
<SomePicker value={value} onChange={(value) => returnValueToA(value)}
);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments