I started adding React components to an existing website. I currently have a simple component that looks like this.
'use strict';
const element = React.createElement
class CourseForm extends React.Component {
constructor(props) {
super(props)
this.state = {
someObject: ''
}
}
changeObject() {
//this value will change on various events
this.setState({ someObject: {key: value} })
}
render() {
return (
<div>
This is a react form!
</div>
)
}
}
const domContainer = document.querySelector('#react-course-form')
ReactDOM.render(element(CourseForm), domContainer)
An html element is used to show the component.
<div id="react-course-form"></div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<!-- Load React component. -->
<script type="text/babel" src="./js/components/CourseTabs.jsx"></script>
<script type="text/babel" src="./js/components/CourseForm.jsx"></script>
What I would like to do is include another componen, and send it data on events from this component.
render() {
return (
<div>
This is a react form!
<button onClick={this.changeObject}>change</button>
<div id="another-component" sendObject="this.state.someObject">
<div id="another-component2" sendObject="this.state.someObject">
</div>
)
}
So I basically want the child components to be aware of what someObject
is because they will be displaying data from that object.