I'm trying to learn reactjs and i have some uncertainties. I was refering react DOCS and some other tutorials and i saw functions are written inside render function and also inside class. What things should we do inside render function in react?
1st way
class App extends Component {
test(user) {
return user.firstName;
}
render() {
const user = {
firstName: 'Harper',
lastName: 'Perez'
};
return (
<div>
<h1>{this.test(user)}</h1>
</div>
)
}
}
2nd way
class App extends Component {
render() {
const user = {
firstName: 'Harper',
lastName: 'Perez'
};
function test(user) {
return user.firstName;
}
return (
<div>
<h1>{test(user)}</h1>
</div>
)
}
}
Both this methods work. But i want to know what is the best method to do this? Most importantly i want to know what kind of things i can do inside render function.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…