"webpack-dev-server/client?http://localhost:8080/"
var instance = ReactDOM.render(<MyComponent />. document.body);
instance.setState({myState: 1});
import io from 'socket.io-client';
export default ({dispatch, getState}) => {
const socket = io();
socket.on('dispatch', action => { dispatch(action) });
return next => action => {
const {type, payload, meta} = action;
if (meta && meta.socket) {
socket.emit(type, payload);
} else {
next(action);
}
};
};
export const handleSubmit = data => ({
type: 'SUBMIT_TO_SERVER',
payload: data,
meta: {socket: true}
});
export const handleChange = event => ({
type: 'CHANGE_VALUE',
payload: event.target.value
});
export default (state = '', {type, payload}) => {
if (type == 'CHANGE_VALUE') return payload;
else return state;
};
import {connect} from 'react-redux';
import {handleChange, handleSubmit} from 'actions';
const Form = ({state, handleChange, handleSubmit}) => <form onChange={handleChange} onSubmit={handleSubmit}>
<input type='text' value={state} />
<button>Submit</button>
</form>;
export default connect(state => state, {handleChange, handleSubmit})(Form);
socket.on('SUBMIT_TO_SERVER', data => {
socket.emit('dispatch', {
type: 'CHANGE_VALUE',
payload: data ? data.toUpperCase() : ''
});
});
class Test extends React.Component {
state = {
text: '',
select: 0
};
handleChange = ({target: {id, value}}) => {
const data = {[id]: value};
if (id == 'text') data.select = value.length % 3;
this.setState(data);
};
render() {
return <div>
<input type='text' id='text' value={this.state.text} onChange={this.handleChange} />
<select id='select' value={this.state.select} onChange={this.handleChange}>
<option value='0'>option1</option>
<option value='1'>option2</option>
<option value='2'>option3</option>
</select>
</div>;
}
}
<button disabled={this.state.checkrule}>
const resetable = Component => class extends React.Component {
state = {key: 0};
render() {
return <Component
{...this.props}
reset={() => this.setState({key: this.state.key + 1})}
key={this.state.key}
/>;
}
}
примерconst resetable = Component => class extends React.Component {
render() {
return <Component
{...this.props}
reset={() => this.forceUpdate()}
key={Math.random()}
/>;
}
}