Đi một thành phần đơn giản:Làm cách nào để hiển thị chuỗi dưới dạng trẻ em trong thành phần React?
function MyComponent({ children }) {
return children;
}
này hoạt động:
ReactDOM.render(<MyComponent><span>Hello</span></MyComponent>, document.getElementById('stage'));
nhưng điều này không (tôi loại bỏ các <span/>
):
ReactDOM.render(<MyComponent>Hello</MyComponent>, document.getElementById('stage'));
vì Phản ứng cố gắng để gọi render
trên chuỗi:
Uncaught TypeError: inst.render is not a function
Mặt khác, điều này hoạt động tốt:
ReactDOM.render(<p>Hello</p>, document.getElementById('stage'));
Làm thế nào để làm cho <MyComponent/>
cư xử như <p/>
?