ReactDOM.render(
<> <HelloMessage name="Zsolt" />
<HelloMessage name="Sarah" /> </>,
document.getElementById('root')
);
NOW I get it
ReactDOM.render(
<> <HelloMessage name="Zsolt" />
<HelloMessage name="Sarah" /> </>,
document.getElementById('root')
);
NOW I get it
const root = document.getElementById('root');
class Greeting extends React.Component {
render(){
return <div>Hello {this.props.name}</div>;
}
}
const user1 = <Greeting name="Paddy"/>;
const user2 = <Greeting name="Tom"/>;
const user3 = <Greeting name="Harry"/>;
var entries = [user1, user2, user3];
ReactDOM.render(
entries,
root
);
Is this correct? I am receiving an error message for not having keys for each list item.
ReactDOM.render(
,
document.getElementById(ārootā)
);
Hi @paddyc1,
The error message that you see is not actually an error but a warning message. React expects any list item to have unique keys attached to each item. Although this wonāt break your code, it is always a good practice to have it.
Hope this clears the confusion.
Hi @kHarsh,
You forgot to pass in an element as the first argument. You can either pass a component or an empty div as well.
Happy Learning!
ReactDOM.render(
<> <HelloMessage name = "Zsolt"/> <HelloMessage name = "Sarah"/> </>,
document.getElementById('root')
);
<div>
<HelloMessage name="Zsolt"/> <HelloMessage name="Sarah"/>
</div>,
document.getElementById('root')
);
The line would be adjusted thus per the error suggestion,
<><HelloMessage name="Pooja" /><HelloMessage name="Pigeon"/></>,
<><HelloMessage name="Zsolt" /><HelloMessage name="Sarah" /></>
ReactDOM.render(
<div>
<HelloMessage name="Zsolt"/>
<HelloMessage name="Sarah"/>
</div>,
document.getElementById('root')
);
ReactDOM.render(
<div>
<HelloMessage name = "Taylor" />,
<HelloMessage name = "Sarah" />,
</div>,
document.getElementById('root')
Itās a good thing that I can easily recognize the errors I make. I was able to solve this exercise with little effort!
<body>
<div id="root"></div>
<script type="text/babel">
class HelloMessage extends React.Component {
render() {
return (
<div>
Hello, {this.props.name}
</div>
);
}
}
ReactDOM.render(
<div>
<HelloMessage name="Zsolt" />
<HelloMessage name="Sarah" />
</div>,
document.getElementById('root')
);
Thanks!
I just put the two HelloMessage tags inside of empty tags <> </>.
In other words: <> <āfirst HelloMessageā /><āsecond HelloMessageā /></>,
ReactDOM.render(
<div>
<HelloMessage name="bdoherty" /><HelloMessage name="mp" />
</div>,
document.getElementById('hello-example')
);
ReactDOM.render(
<> <HelloMessage name="Zsolt"/> <HelloMessage name="Sarah"/> </>,
document.getElementById("root")
);
Exercise: Rendering two Elements
Given elements
<HelloMessage name="Zsolt" /><HelloMessage name="Sarah" />
ReactDOM.render() call
ReactDOM.render(
<>
<HelloMessage name="Zsolt" />
<HelloMessage name="Sarah" /> </>,
document.getElementById('helloMessage')
);
<div id="hello-example"></div>
<script type="text/babel">
class HelloMessage extends React.Component {
render() {
return (
<div>
Hello, {this.props.name}!
Hello, {this.props.name2}!
</div>
);
}
}
ReactDOM.render(
<HelloMessage name="Zsolt" name2="Sarah"/>,
document.getElementById('hello-example')
);
</script>
Please suggest ways to improve this, thanks!
The elements are required to be wrapped by a set of tags, eg <div></div>
however there is a shorter way with an unnamed set of tags by using <></>
.
reactDOM.render(
<>
<HelloMessage name="Zsolt" />
<HelloMessage name="Sarah" />
</>,
document.getElementById('root')
);
class HelloMessage extends React.Component {
render() {
return (
<div>
Hello {this.props.name} and {this.props.name2}
</div>
);
}
}
ReactDOM.render(
<HelloMessage name="Hayden" name2 = "Sara"/>,
document.getElementById('root')
);
ReactDOM.render(
<><HelloMessage name="Taylor" /><HelloMessage name="Sarah" /></>,
document.getElementById('root')
);