小编Ale*_*ing的帖子

如何使用 Enter 提交 Semantic React Search

我正在尝试设置我的搜索,因此当我单击 Enter 时,它将开始搜索并重定向到搜索页面。我正在查看文档,但不清楚如何设置。如何设置按 Enter 开始搜索?我很难弄清楚这一点,尽管我认为它应该很简单。

class SearchBar extends Component {
  constructor(props) {
    super(props)
    this.state = {query: '', results: [], isLoading: false}
  }

  componentWillMount() {
     this.resetComponent()
   }

   resetComponent = () => this.setState({ isLoading: false, results: [], value: '' })

   search(query) {
     this.setState({ query });
     axios
       .get(`/api/search?query=${query}`)
       .then(response => {
         console.log(query);
         this.setState({ results: response.data});
       })
       .catch(error => console.log(error));
   }


  handleSearchChange = (query) => {
    this.search(query);
    this.setState({ isLoading: true, query })

    setTimeout(() =>
      this.setState({
        isLoading: false,
      }) , 300)

  }

  handleResultSelect = (e, …
Run Code Online (Sandbox Code Playgroud)

reactjs semantic-ui-react

7
推荐指数
1
解决办法
5576
查看次数

使用 JSON 通过 Rails Active Storage 访问 React 中的图像

我正在制作一个 Rails API,我想使用 Active Storage 将图像附加到帖子,然后能够在 REACT 中访问它们,例如获取 JSON 中的 url 链接。如何将 Active Storage 图像转换为 JSON 的 url。有没有更好的方法可以获取帖子图像的链接?

例如,我希望图像 url 包含在此信息中:

来自:http://localhost:3001/api/posts

[{"id":8,"title":"lolol","body":"lolol","created_at":"2018-07-19T23:36:27.880Z","updated_at":"2018-07-20T00:17:50.201Z","admin_user_id":1,"post_type":"Song","link":"dgdadg","song_title":"dgdadg","tag_list":[]},{"id":13,"title":"ddd","body":"dd","created_at":"2018-07-20T00:21:39.903Z","updated_at":"2018-07-20T00:21:39.907Z","admin_user_id":1,"post_type":"Song","link":"dddd","song_title":"ddd","tag_list":["Tag"]}]
Run Code Online (Sandbox Code Playgroud)

这是我的帖子控制器:

class PostsController < ApiController
    before_action :set_post, only: [:show, :update, :destroy]

  def index
    if params[:tag]
      @posts = Post.tagged_with(params[:tag])
    else
      @posts = Post.all
    end

    render :json => @posts
  end

  def show
    @post
    render :json => @post
  end

  def create
    @post = Post.new(post_params)
  end

  def update
    @post.update(post_params)
    head :no_content
  end

  def destroy
    @post.destroy
    head :no_content
  end

  private

  def …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails reactjs rails-activestorage

5
推荐指数
1
解决办法
1600
查看次数

无法从 REACT 中的 JSON 调用访问单个对象中的嵌套对象属性

我正在调用 Rails API 并返回一个具有嵌套用户对象和标签列表数组的 JSON 对象。但是,我无法访问嵌套的对象。

this.props.post.user.name 抛出:无法读取未定义的属性“名称”。

我很困惑,因为当我在 PostsIndex.js 中调用 PostsIndex 并获取对象数组并通过它映射时,我可以访问所有内容。

只处理单个对象时我需要做些什么吗?

PostShow.js

import React, {Component} from 'react';
import axios from 'axios';
import {Link} from 'react-router-dom';



export default class PostShow extends Component {

constructor(props) {
  super(props)
  this.state = {
    post: {}
  };
}

componentDidMount() {
  const { match: { params } } = this.props;
  axios
    .get(`/api/posts/${params.postId}`)
    .then(response => {
      console.log(response);
      this.setState({ post: response.data});
    })
    .catch(error => console.log(error));

}

  render() {
    return (
      <div>
            <Post post={this.state.post}/>
      </div>
    );
  } …
Run Code Online (Sandbox Code Playgroud)

javascript json reactjs

4
推荐指数
1
解决办法
2642
查看次数