๋ ์ฌ์ฉ dispatch
์ ๊ธฐ๋ฅ์ onChange
๊ธฐ๋ฅ์ ๊ฐ์น๋ฅผ ๊ธฐ๋ํ์ง ์์ต๋๋ค.
this.editor.value
๋ null์ด๋ฏ๋ก ์ฌ๊ธฐ์ ์๋ฌด ๊ฒ๋ ๊ฐ์ ธ์ค์ง ์์ต๋๋ค.
๋ฒ์ : 3.0.0
๋ฐ๋ชจ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import brace from 'brace';
import AceEditor from 'react-ace';
import 'brace/mode/sql';
import 'brace/mode/python';
import 'brace/theme/github';
// ActionCreators
export function onChangeAction(value) {
// use `console.log(value)` will not get the right expect value, an input print a letter.
return {
type: 'ON_CHANGE',
text: value,
}
};
// Editor Component
class Editor extends React.Component {
constructor(props) {
super(props);
};
render() {
const { onChange } = this.props;
return (
<AceEditor
mode="sql"
theme="github"
onChange={onChange}
name="UNIQUE_ID_OF_DIV"
width='100%'
ref='editor'
fontSize={15}
editorProps={{$blockScrolling: true}}
/>
);
}
}
function mapDispatchToProps(dispatch) {
return {onChange: bindActionCreators(onChangeAction, dispatch)};
};
connect(
mapDispatchToProps
)(Editor)
// App
import { Provider } from 'react-redux';
class App extends React.Component {
render() {
const { store } = this.props;
return (
<Provider store={store}>
<Editor />
</Provider>
);
}
}
๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์๋ํ์ง ์์ต๋๋ค.
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import brace from 'brace';
import AceEditor from 'react-ace';
import 'brace/mode/sql';
import 'brace/mode/python';
import 'brace/theme/github';
// `onChangeAction` from ActionCreators
export function onChangeAction(text) {
return {
type: 'ON_CHANGE',
statement: text,
}
};
class Editor extends React.Component {
constructor(props) {
super(props);
};
onChange = (value) => {
console.log(value);
const { dispatch } = this.props;
dispatch(onChangeAction(value));
};
render() {
return (
<AceEditor
mode="sql"
theme="github"
onChange={this.onChange}
name="UNIQUE_ID_OF_DIV"
width='100%'
ref='editor'
fontSize={15}
editorProps={{$blockScrolling: true}}
/>
);
}
}
connect(
mapStateToProps,
mapDispatchToProps
)(Editor)
class App extends React.Component {
render() {
const { store } = this.props;
return (
<Provider store={store}>
<Editor />
</Provider>
);
}
}
@์์
์ฒซ ๋ฒ์งธ ์์์
connect(
mapDispatchToProps
)(Editor)
ํด์ผํ๋ค
connect(
null,
mapDispatchToProps
)(Editor)
๋ ๋ฒ์งธ ์์์๋ mapStateToProps ๋ฐ mapDispatchToProps ํจ์ ๊ตฌํ์ด ์์ต๋๋ค.
@chentsulin
๋ ๋ฒ์งธ ์์์๋ 'mapDispatchToProps'๊ฐ ํ์ํ์ง ์์ต๋๋ค.
const mapStateToProps = (state) => {
return {
query: getQuery(state),
}
};
๋๋ ์ญ์ componentWillReceiveProps
ํจ์ https://github.com/securingsincity/react-ace/blob/master/src/ace.jsx
, ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ์๋ํฉ๋๋ค.
๊ทธ๋ ์ง ์์ผ๋ฉด 'insert' ํธ์ถ ํ 'delete' ์์
์ ํธ์ถํฉ๋๋ค. componentWillReceiveProps
ํจ์์ console.log(nextProps)์์ ์ฐพ์ ์ ์์ต๋๋ค.
๋ฌผ๋ก ๋์คํจ์น๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ํ์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค.
๋๋ ์ด๊ฒ์ ์ก์ง ๋ชปํ๋ค. delete
๋ฐ insert
์์
์ ์ด๋์ ์์ต๋๊น?
@chentsulin ace.jsx
๋ฅผ ๋ด ํ๋ก์ ํธ์ ๋ณต์ฌํ๊ณ ReactAce.componentWillReceiveProps์ console.log(nextProps)๋ฅผ ๋ณต์ฌํ์ต๋๋ค.
๋๊ตฐ๊ฐ ๊ทธ๊ฒ์๋ณด๊ณ ์์ต๋๊น?
์ด๊ฒ์ ์ฌ์ ํ โโ๋์๊ฒ ๋ฌธ์ ์
๋๋ค
์ ๊ฒฝ์ฐ์๋ ์ํ ๊ฐ์ value
์ต์
์ผ๋ก ์ค์ ํฉ๋๋ค.
const TextArea = (props) => (
<div className={styles.box}>
<AceEditor
mode="javascript"
theme="solarized_light"
onChange={props.onChange}
name="RIGHT_INPUT"
width="100%"
height="100%"
value={props.value}
showPrintMargin={false}
editorProps={{ $blockScrolling: true }}
/>
</div>
);
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ ๊ฒฝ์ฐ์๋ ์ํ ๊ฐ์
value
์ต์ ์ผ๋ก ์ค์ ํฉ๋๋ค.