1

I'm currently trying to use an angular feature, but I cant use ng-click inside an a react app.

I am trying to convert the following function to a react function, I'm aware of axios, not sure how to approach blade variables in react.

I need the angular snippet to do be converted into a react function.

Angular(main.js)

$scope.myfollow = function(user) {
    $http.post('/user/follow/'+ user.id).then(function(result) {
        console.log("user id is:"+ user.id);
    });
};

React file

render(){
    return(

       <div className="followdoe">      
            <button  onClick={ this.btnClick.bind(this) } className={this.state.className}>
                   <p>{ this.state.btnText }</p>
            </button>
       </div>


    )
}

Route.php

Route::post('user/follow/{id}', 'UserController@my_follow');

Profile.blade.php

<div style="margin:30px 0px;">

    <button class="follow-button" ng-click="myfollow({{$user}});">+ Follow</button>
</div>

UserController.php

    public function my_follow(Request $request, $id)
    {
        $user = auth()->user();

        if($user->id != $id && $otherUser = User::find($id)){

            $user->toggleFollow($otherUser);
        }

    }
   public function getProfile($user)
    {  
        $user = User::with(['posts.likes' => function($query) {
                            $query->whereNull('deleted_at');
                        }, 'follow','follow.follower'])
                      ->where('name','=', $user)->first();



        if(!$user){
            return redirect('404');
        }

        return view ('profile')->with('user', $user);
    }
4
  • Do you have the user data in your React component? Commented Jul 7, 2018 at 23:23
  • i don't tholle, how would i do that Commented Jul 7, 2018 at 23:27
  • Your question is about how you should port your angular myfollow function to React. I'm not sure how your data looks beyond that, I'm afraid. Commented Jul 7, 2018 at 23:28
  • 1
    What data ? i just want a way to make the same angular function but in react, would you have an idea on how to do that ? thanks tholle Commented Jul 7, 2018 at 23:31

1 Answer 1

1

You could create a function on your component and use the fetch API.

Example

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      className: "test",
      btnText: "Text",
      user: { id: 123 }
    };
    this.myfollow = this.myfollow.bind(this);
    this.btnClick = this.btnClick.bind(this);
  }

  myfollow(user) {
    fetch(`/user/follow/${user.id}`, { method: "POST" })
      .then(response => response.json())
      .then(response => {
        console.log(response);
      });
  };

  btnClick() {
    this.myfollow(this.state.user);
  };

  render() {
    return (
      <div className="followdoe">
        <button onClick={this.btnClick} className={this.state.className}>
          <p>{this.state.btnText}</p>
        </button>
      </div>
    );
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

the my follow function is giving a syntax error, im trying to figure it out
unexpected token somewhere near myfollow = user
@BARNOWL Alright. I updated the answer. You might not have class fields in your development environment, which my previous version of the answer used.
perfect, it build successful let me give this a shot.
@BARNOWL Yes, that's what my second question above was about. Do you have the user data in your React component? Since you use user.id in your angular code, you need the user.id for the URL.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.