I'm new to reactjs. I called this libraries in head tag:
<script src="react/build/react.js"></script>
<script src="react/build/react-dom.js"></script>
<script src="https://unpkg.com/[email protected]/browser.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/15.8.23/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="chat-component.js" type="text/babel"></script>
and this is my react code:
var ChatApp = React.createClass({
getInitialState: function () {
return {
messages: [],
socket: window.io('http://localhost:3000')
}
},
componentDidMount: function () {
var self = this;
this.state.socket.on("receive-message", function (msg) {
console.log(msg);
self.setState({messages: self.state.messages.push(msg)})
});
},
render: function () {
return (
<div>
<input id="message" type="text"/>
<button type="button" onClick={this.submitMessage} id="demo" value="send">send</button>
</div>
)
},
submitMessage: function () {
var message = $('#message').value;
// this.socket.emit("new-message", message);
console.log(message);
},
});
ReactDOM.render(
<ChatApp />,
document.getElementById("chat")
);
Why Button onClick does not working in my code? also in inspector, onClick does not exist :
<button type="button" id="demo" value="send">send</button>
I know my question is repeated but I really don't find its solution.
onChangeto your input so that you can update the state as the user types. Once the button is clicked, you can then simply just reference the value in your state. Here's the classical example from the reactjs docs: codepen.io/ericnakagawa/pen/QKkJva?editors=0010