I reviewed the code that you pasted here and I found the following:
const App = ()=>{
//...some code
functionA() {
//...some code
}
functionA() {
//...some code
}
return(
<div>
<A runWhenChanged = {functionA} />
<B runWhenChanged = {functionB} />
</div>
)
}
//... some code
Here, I don't know if it was a mistake when you where editing the post or it's the literal copy of your code. But on the functions declarations, you have two functions called functionA. There is no functionB that is being passed to the B component, so you are passing an undeclared function.
I would recommend checking that and turning those functions into arrow functions.
constant B = ({runWhenChanged})=>{
const [term,setTerm] = useState('');
constant runOnChange = (event)=>{
setTerm(event.target.value);
runWhenChanged(term);
}
return(
<div>
<input type = "text" onChange = {runOnChange} value = {term}/>
<div>
);
}
For this next piece of code I see that you declared your component with constant and also runOnChange; you should switch it to const. At the return, the div doesn't have a properly closed tag.
Aside that, I think the problem is with the function declarations, what is being called inside them or how they are being passed to the components. I duplicated your code applying the observations I just wrote and everything is working fine. Here is my code:
App component:
import React from "react";
import A from "./A";
import B from "./B";
function App() {
const functionA = () => {
console.log("Changed value in A");
}
const functionB = () => {
console.log("Changed value in B");
}
return (
<div>
<A runWhenChanged = {functionA} />
<B runWhenChanged = {functionB} />
</div>
);
}
export default App;
A component:
import React, { useState } from "react";
const A = ({runWhenChanged}) => {
const [term,setTerm] = useState('');
const runOnChange = (event)=>{
setTerm(event.target.value);
runWhenChanged(term);
}
return(
<div>
<input type = "text" onChange = {runOnChange} value = {term}/>
</div>
);
}
export default A;
B component:
import React, { useState } from "react";
const B = ({runWhenChanged}) => {
const [term,setTerm] = useState('');
const runOnChange = (event) =>{
setTerm(event.target.value);
runWhenChanged(term);
}
return(
<div>
<input type = "text" onChange = {runOnChange} value = {term}/>
</div>
);
}
export default B;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…