React-ace: لا يمكن الحصول على القيمة المتوقعة عند استخدام إرسال Redux

تم إنشاؤها على ١٠ يناير ٢٠١٦  ·  7تعليقات  ·  مصدر: securingsincity/react-ace

عند استخدام دالة dispatch في دالة onChange ، لن تحصل على القيمة المتوقعة.

this.editor.value فارغ ، لذا لا تحصل على شيء هنا.

الإصدار: 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>
    );
  }
}

التعليق الأكثر فائدة

في حالتي ، اضبط قيمة الحالة على الخيار 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>
);

ال 7 كومينتر

لا يعمل بهذه الطريقة:

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.

تضمين التغريدة

لا يحتاج إلى "mapDispatchToProps" في المثال الثاني.

const mapStateToProps = (state) => {
  return {
    query: getQuery(state),
  }
};

قمت بحذف الدالة componentWillReceiveProps في https://github.com/securingsincity/react-ace/blob/master/src/ace.jsx ، وهي تعمل.

إذا لم يكن الأمر كذلك ، فسيتم استدعاء إجراء "حذف" بعد استدعاء "إدراج". يمكنك العثور عليه عن طريق console.log (nextProps) في وظيفة componentWillReceiveProps .

بالتأكيد ، سيتم تمرير الإرسال كدعائم افتراضيًا.

أنا لا أفهم هذا. أين هي الإجراءات delete و insert ؟

chentsulin لقد نسخت ace.jsx إلى مشروعي الخاص ، و console.log (nextProps) في ReactAce.componentWillReceiveProps.

هل هناك من ينظر اليه؟
هذا لا يزال يمثل مشكلة بالنسبة لي

في حالتي ، اضبط قيمة الحالة على الخيار 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>
);
هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات