我有一个针对装饰器的策略功能,我想将道具传递给策略功能 我已经尝试了下面的代码,但它无法读取未定义的属性道具
const highlightWorngWords = (contentBlock, callback , props ) => {
let text = contentBlock.getText();
let worngWords = props.errorWordslist ? props.wongwords : [] ;
console.log('wong words', worngWords);
let start ;
worngWords.forEach(word => {
start = text.indexOf(word);
if (start !== -1) {
callback(start, start + word.length);
}
})
}
道具只是一个单词数组,我是从mapStateToProps那里得到的,我确实尝试在函数中赋值并起作用 但是当我想将道具传递给函数时,它将无法工作 知道如何将道具传递给它
import React, { Component , Fragment} from 'react';
import { connect } from 'react-redux';
import * as actionCreators from '../../store/actions/index';
import Footer from '../Footer/Footer';
import { Editor, EditorState , CompositeDecorator } from 'draft-js';
import HighlightedWords from './MyComponent'
var h2p = require('html2plaintext');
class Main extends Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(compositeDecorator),
subjectTitle: "",
}
this.handleChange = this.handleChange.bind(this);
this.animEditor = this.animEditor.bind(this);
}
handleChange = (editorState) => {
let TextValue = editorState.getCurrentContent().getPlainText();
this.setState({
editorState,
});
var text = h2p(editorState.getCurrentContent().getPlainText());
console.log("satet" , text);
this.props.onChangeText(text);
this.props.onSpellCheck(text);
}
return(
<Fragment>
<main id="main-container" className="main_container">
<section data-simplebar="init" id="editor-wrap" className="editor_wrap" ss-container='true'>
<div
id="dhad-editor"
className="dhad_editor"
>
<Editor
editorState={this.state.editorState}
onChange={this.handleChange}
/>
</div>
</section>
</main>
</Fragment>
);
}
}
const highlightWorngWords = (contentBlock, callback) => {
let text = contentBlock.getText();
let worngWords = this.props.errorWordslist ? this.props.errorWordslist:[];
console.log('wong words', worngWords);
let start ;
worngWords.forEach(word => {
start = text.indexOf(word);
if (start !== -1) {
callback(start, start + word.length);
}
})
}
const compositeDecorator = new CompositeDecorator([
{
strategy: highlightWorngWords.bind(this), //here
component: HighlightedWords ,
}
]);
const mapStateToProps = state => {
return {
text: state.edi.text ,
errorWordslist: state.edi.errorWordslist,
correctionsList: state.edi.correctionsList
};
};
const mapDispatchToProps = dispatch => {
return {
onStart: () => dispatch(actionCreators.start()),
onChangeText: (value) => dispatch(actionCreators.TextChange(value)),
onSpellCheck: (word) => dispatch(actionCreators.ErorrWordsList(word)),
}
};
export default connect( mapStateToProps , mapDispatchToProps ) (Main);
答案 0 :(得分:1)
道具只是我从中得到的一系列单词 mapStateToProps
在组件类中添加函数,并保留箭头函数声明以自动绑定组件上下文。
Document.Status
,然后修复构造函数中的装饰器声明以指向namespace
const onStateChange = (selectedOption, {action}) => {
//do something with the selectedOption according to the action
}
<Select onChange={onStateChange} />