gpt4 book ai didi

javascript - Reactjs Prop 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 15:10:14 25 4
gpt4 key购买 nike

var Works = function welcome(props) {
return <h1> Hello, {props.name} </h1>;
}

/*function welcome() {
return <h1>Hello, {this.props.name}</h1>;
}*/

const element = (<div>
<Works name="Luffy" />
</div>);

ReactDOM.render(
element,
document.getElementById('root')
);

如果我用没有传递 Prop 的注释部分替换 Works 组件,它会出错,我需要知道为什么在使用类时我们直接使用 this.props 时会发生这种情况。为什么不在这里?

最佳答案

你应该在Function之后定义组件的名字:

function Works(props) {
return <h1 > Hello, { props.name } < /h1>;
}

希望对您有所帮助!

关于javascript - Reactjs Prop 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45212010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com