2
mufaでsub/pubパターンを使用して、小道具の代わりに反応コンポーネント間を通信しています。次に、Parentコンポーネントのロジックを緩和します(次のスニペットをご覧ください)。条件の後にmufaを使用して反応成分間の通信を停止する
const {on, fire} = mufa;
class Input extends React.Component {
onChange(event) {
fire('input_change', event.target.value);
}
render() {
return <input onChange={this.onChange.bind(this)} />
}
}
class Label extends React.Component {
state= {text: ""};
componentDidMount() {
on('input_change', this.onInputChange.bind(this));
}
onInputChange(inputValue) {
this.setState({text: inputValue});
// I need code to stop calling this method when inputValue reaches 10 characters.
}
render() {
return <label > {this.state.text} </label>
}
}
class App extends React.Component {
// No logic here thanks to the Sub/Pub pattern.
render() {
return (
<div>
<Label />
<Input/>
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdn.rawgit.com/abdennour/mufa/ddf78fd9/cdn/mufa-latest.min.js"></script>
<div id="app" ></div>
私の懸念は、入力値が10個の文字に達したときLabel
とInput
間でこの通信を停止する方法です。
onInputChange(inputValue) {
if (inputValue.length <= 10) // <-- I add this.
this.setState({text: inputValue});
}
しかし、この条件はonInputChange
の呼び出しを防ぐことはできません。
input_change
イベントへ)。
mufa
はこのように退会する方法を持っているよう
あなたは天才です..それは魔法のように動作..ありがとうございました。 私はあなたの答えを投票することができません。それは少なくとも15ポイントを必要とstackoverflowのようです。とにかく、多くのありがとう。 – rathathhub
'unsub'または' off':[documentation](http://elegance.abdennoor.com/mufa/#off)に従って2つのメソッドが有効です。 –