来段setup父子传值代码
时间: 2023-03-28 22:03:46 浏览: 147
vue-子父组件传值源代码
好的,以下是一个简单的父子传值的示例代码:
// 父组件
import React, { useState } from 'react';
import ChildComponent from './ChildComponent';
function ParentComponent() {
const [value, setValue] = useState('');
const handleValueChange = (newValue) => {
setValue(newValue);
};
return (
<div>
<ChildComponent onValueChange={handleValueChange} />
<p>父组件的值: {value}</p>
</div>
);
}
export default ParentComponent;
// 子组件
import React, { useState } from 'react';
function ChildComponent(props) {
const [value, setValue] = useState('');
const handleInputChange = (event) => {
const newValue = event.target.value;
setValue(newValue);
props.onValueChange(newValue);
};
return (
<div>
<label>子组件的值:</label>
<input type="text" value={value} onChange={handleInputChange} />
</div>
);
}
export default ChildComponent;
阅读全文