我完全是新的反应,我在ES5做我的项目。我需要使用React Autosuggest
模块,但它是ES6中的示例。
任何人都可以将其翻译为ES5
function states () {
return [
{abbr: 'AL', name: 'Alabama'},
{abbr: 'AK', name: 'Alaska'},
{abbr: 'AZ', name: 'Arizona'},
{abbr: 'AR', name: 'Arkansas'},
]
}
class AutocompleteExample extends React.Component {
constructor () {
super()
this.state = {tags: []}
}
handleChange (tags) {
this.setState({tags})
}
render () {
function autocompleteRenderInput ({addTag, ...props}) {
const handleOnChange = (e, {newValue, method}) => {
if (method === 'enter') {
e.preventDefault()
} else {
props.onChange(e)
}
}
const inputValue = (props.value && props.value.trim().toLowerCase()) || ''
const inputLength = inputValue.length
let suggestions = states().filter((state) => {
return state.name.toLowerCase().slice(0, inputLength) === inputValue
})
return (
<Autosuggest
ref={props.ref}
suggestions={suggestions}
shouldRenderSuggestions={(value) => value && value.trim().length > 0}
getSuggestionValue={(suggestion) => suggestion.name}
renderSuggestion={(suggestion) => <span>{suggestion.name}</span>}
inputProps={{...props, onChange: handleOnChange}}
onSuggestionSelected={(e, {suggestion}) => {
addTag(suggestion.name)
}}
onSuggestionsClearRequested={() => {}}
onSuggestionsFetchRequested={() => {}}
/>
)
}
return <TagsInput renderInput={autocompleteRenderInput} value={this.state.tags} onChange={::this.handleChange} />
}
}
答案 0 :(得分:1)
您可以使用babel来转换代码。您可以使用babel repl执行此操作。这是代码的转换版本
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function states() {
return [{ abbr: 'AL', name: 'Alabama' }, { abbr: 'AK', name: 'Alaska' }, { abbr: 'AZ', name: 'Arizona' }, { abbr: 'AR', name: 'Arkansas' }];
}
var AutocompleteExample = function (_React$Component) {
_inherits(AutocompleteExample, _React$Component);
function AutocompleteExample() {
_classCallCheck(this, AutocompleteExample);
var _this = _possibleConstructorReturn(this, (AutocompleteExample.__proto__ || Object.getPrototypeOf(AutocompleteExample)).call(this));
_this.state = { tags: [] };
return _this;
}
_createClass(AutocompleteExample, [{
key: 'handleChange',
value: function handleChange(tags) {
this.setState({ tags: tags });
}
}, {
key: 'render',
value: function render() {
function autocompleteRenderInput(_ref) {
var addTag = _ref.addTag,
props = _objectWithoutProperties(_ref, ['addTag']);
var handleOnChange = function handleOnChange(e, _ref2) {
var newValue = _ref2.newValue,
method = _ref2.method;
if (method === 'enter') {
e.preventDefault();
} else {
props.onChange(e);
}
};
var inputValue = props.value && props.value.trim().toLowerCase() || '';
var inputLength = inputValue.length;
var suggestions = states().filter(function (state) {
return state.name.toLowerCase().slice(0, inputLength) === inputValue;
});
return React.createElement(Autosuggest, {
ref: props.ref,
suggestions: suggestions,
shouldRenderSuggestions: function shouldRenderSuggestions(value) {
return value && value.trim().length > 0;
},
getSuggestionValue: function getSuggestionValue(suggestion) {
return suggestion.name;
},
renderSuggestion: function renderSuggestion(suggestion) {
return React.createElement(
'span',
null,
suggestion.name
);
},
inputProps: _extends({}, props, { onChange: handleOnChange }),
onSuggestionSelected: function onSuggestionSelected(e, _ref3) {
var suggestion = _ref3.suggestion;
addTag(suggestion.name);
},
onSuggestionsClearRequested: function onSuggestionsClearRequested() {},
onSuggestionsFetchRequested: function onSuggestionsFetchRequested() {}
});
}
return React.createElement(TagsInput, { renderInput: autocompleteRenderInput, value: this.state.tags, onChange: this.handleChange });
}
}]);
return AutocompleteExample;
}(React.Component);
答案 1 :(得分:0)
没有将React组件直接转换为ES5。在ES6之前的反应有自己的React.createClass
语法和其他类似的东西。
它具有欺骗性,因为它使用ES6 class
语法,但React有自己独特的处理方式。所有的魔法都在extends Component
内。你感到困惑的一切可能都是.jsx
和render
之类的东西(它是一个React的东西)。
Babel将React组件转换成您永远不会写的东西。这不是你想要的(我认为)。如果类语法让你感到困惑。独立学习,然后在React的背景下处理ES6。