React-ace: No se puede obtener el valor esperado cuando se usa el envío de Redux

Creado en 10 ene. 2016  ·  7Comentarios  ·  Fuente: securingsincity/react-ace

Cuando use la función dispatch en la función onChange , no obtendrá el valor esperado.

this.editor.value es nulo, así que no obtenga nada aquí.

Versión: 3.0.0

Aquí está la demostración:

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>
    );
  }
}

Comentario más útil

En mi caso, establezca el valor del estado en la opción 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>
);

Todos 7 comentarios

No funciona de esta manera:

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>
    );
  }
}

@Yuanye

En tu primer ejemplo

connect(
  mapDispatchToProps
)(Editor)

debiera ser

connect(
  null,
  mapDispatchToProps
)(Editor)

En su segundo ejemplo, no tiene un implemento de función mapStateToProps y mapDispatchToProps.

@chentsulin

No necesita 'mapDispatchToProps' en el segundo ejemplo.

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

Elimino la función componentWillReceiveProps en https://github.com/securingsincity/react-ace/blob/master/src/ace.jsx , y funciona.

De lo contrario, llamará a la acción 'eliminar' después de llamar a 'insertar'. Puede encontrarlo por console.log (nextProps) en la función componentWillReceiveProps .

Claro, el envío se transmitirá como accesorios de forma predeterminada.

No entiendo esto. ¿Dónde están las acciones delete y insert ?

@chentsulin Copié ace.jsx en mi propio proyecto y console.log (nextProps) en ReactAce.componentWillReceiveProps.

¿Alguien lo está mirando?
esto sigue siendo un problema para mí

En mi caso, establezca el valor del estado en la opción 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>
);
¿Fue útil esta página
0 / 5 - 0 calificaciones

Temas relacionados

dmavrin picture dmavrin  ·  3Comentarios

SecMao picture SecMao  ·  4Comentarios

huangjiatian picture huangjiatian  ·  7Comentarios

anderoonies picture anderoonies  ·  5Comentarios

kolbinski picture kolbinski  ·  5Comentarios