Я использую камень React-Rails и получаю доступ к объекту json items
с контроллера Rails.Реакция json Объекты недействительны в качестве дочернего элемента React
Rails контроллер:
class ItemsController < ApplicationController
def index
@items = Item.all
render json: @items
end
end
Мой Реагировать App
компонент получают доступ к этим пунктам и пытаются передать его в качестве опоры для ребенка компонента:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
items: {},
activeTab: 'items'
};
}
componentDidMount() {
$.getJSON('/items.json', (response) => {
this.setState({ items: response })
});
}
render() {
return (
<div>
<ItemsContent items={this.state.items}>
</div>
);
}
}
И этот ребенок компонента выглядит следующим образом:
class ItemsContent extends React.Component {
render() {
return (
<div>
<div>Items: {this.props.items}</div>
</div>
);
}
}
ItemsContent.propTypes = {
items: React.PropTypes.object
};
И вот эта ошибка:
react.js?body=1:1324 Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `ItemsContent`.
Как обойти это? Есть ли способ легко использовать объекты JSON в моих компонентах React?
Прямо сейчас я попытался обертывание объект JSON в массив:
tabbedContent = <ItemsContent items={[this.state.items]}></ItemsContent>;
Вы пытаетесь отобразить все элементы в this.props.items? – erichardson30
Да, я пытаюсь отобразить все предметы! – user2082000